[英]Getting name is not defined error when trying to import a video to my cv2 face emotion recognition code
我正在嘗試使用 opencv 制作面部情感識別項目。 我可以通過我的網絡攝像頭使用和預測情緒(使用此代碼:cv2.VideoCapture(0))。 但出於演示目的,我想將網絡攝像頭更改為特定的 mp4 視頻。 但我得到一個錯誤。 這是我的代碼:
path = "haarcascade_frontalface_default.xml"
font_scale = 1.5
font = cv2.FONT_HERSHEY_PLAIN
#set the rectangle background to white
rectangle_bgr = (255, 255, 255)
# make a black image
img = np.zeros((500, 500))
#set some text
text = "VİDGA Projesi"
#get the width and height of the text box
(text_width, text_height) = cv2.getTextSize(text, font, fontScale=font_scale, thickness=1)[0]
#set the text start position
text_offset_x = 10
text_offset_y = img.shape[0] - 25
#make the coords of the box with a small padding of two pixels
box_coords = ((text_offset_x, text_offset_y), (text_offset_x + text_width + 2, text_offset_y - text_height -2))
cv2.rectangle(img, box_coords[0], box_coords[1], rectangle_bgr, cv2.FILLED)
cv2.putText(img, text, (text_offset_x, text_offset_y), font, fontScale=font_scale, color= (0,0,0), thickness=1)
cap = cv2.VideoCapture("sample.mp4")
#Check if the webcam is opened correctly
#if not cap.isOpened():
#cap = cv2.VideoCapture(0)
if not cap.isOpened():
raise IOError("Cannot open video")
while True:
ret, frame = cap.read()
#eye_Cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_eye.xml')
faceCascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
if ret == True:
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
#print(faceCascade.empty())
faces = faceCascade.detectMultiScale(gray,1.1,4)
for x,y,w,h in faces:
print("Alındı.")
roi_gray = gray[y:y+h, x:x+w]
roi_color = frame[y:y+h, x:x+w]
cv2.rectangle(frame, (x,y), (x+w, y+h), (255, 0, 0), 2)
facess = faceCascade.detectMultiScale(roi_gray)
if len(facess) == 0:
print("Face not detected")
else:
for(ex,ey,ew,eh) in facess:
face_roi = roi_color[ey: ey+eh, ex: ex+ew] ##cropping the face
graytemp = cv2.cvtColor(face_roi, cv2.COLOR_BGR2GRAY)
final_image = cv2.resize(graytemp, (48,48))
final_image = np.expand_dims(final_image, axis =0) #add third dimension
final_image = np.expand_dims(final_image, axis =0) #add fourth dimension
final_image = final_image/255.0 # normalization
dataa = torch.from_numpy(final_image)
dataa = dataa.type(torch.FloatTensor)
dataa = dataa.to(device)
outputs = net(dataa)
Pred = F.softmax(outputs, dim=1)
Predictions = torch.argmax(Pred).item()
print(Predictions)
這是我得到的錯誤:
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-5-d57dc2ffd8f1> in <module>
48 face_roi = roi_color[ey: ey+eh, ex: ex+ew] ##cropping the face
49
---> 50 graytemp = cv2.cvtColor(face_roi, cv2.COLOR_BGR2GRAY)
51 final_image = cv2.resize(graytemp, (48,48))
52 final_image = np.expand_dims(final_image, axis =0) #add third dimension
NameError: name 'face_roi' is not defined
當我將 cv2.VideoCapture("sample.mp4") 更改為 cv2.VideoCapture(0) 時,一切都像魅力一樣。 請幫我:)
您只在if ret == True
塊內的else
塊內定義了face_roi
變量。 如果face_roi
成功檢測到一次,那么一切都很好,因為即使在未能重新定義變量的循環中,它也會有之前定義的變量。
只需將while
循環的最后一部分縮進為else
塊(以及else
塊內的for
循環)的一部分:
else:
for(ex,ey,ew,eh) in facess:
face_roi = roi_color[ey: ey+eh, ex: ex+ew] ##cropping the face
graytemp = cv2.cvtColor(face_roi, cv2.COLOR_BGR2GRAY)
final_image = cv2.resize(graytemp, (48,48))
final_image = np.expand_dims(final_image, axis =0) #add third dimension
final_image = np.expand_dims(final_image, axis =0) #add fourth dimension
final_image = final_image/255.0 # normalization
dataa = torch.from_numpy(final_image)
dataa = dataa.type(torch.FloatTensor)
dataa = dataa.to(device)
outputs = net(dataa)
Pred = F.softmax(outputs, dim=1)
Predictions = torch.argmax(Pred).item()
print(Predictions)
為什么不用deepface? 運行以下代碼片段,它會處理所有事情。
#!pip install deepface
from deepface import DeepFace
DeepFace.stream()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.