OpenCv摄像头窗口未打开,仅摄像头指示灯亮起

blmhpbnm  于 2023-03-19  发布在  其他
关注(0)|答案(2)|浏览(249)

你好,我正在进行面部表情识别,我正在使用网络摄像头进行实时视频输入,为了实现这一点,我使用opencv,当我运行代码时,网络摄像头的灯光会发光,但无法获得网络摄像头窗口,虽然没有抛出错误,但仍然无法获得摄像头窗口,只有网络摄像头的灯光会发光。
下面是我的代码:

import cv2
path='haarcascade_frontalface_Default.xml'
font_scale=1.5
font=cv2.FONT_HERSHEY_PLAIN

rectangle_bgr=(255,255,255)
img=np.zeros((500,500))

text="Text is written for box"
(text_width,text_height)=cv2.getTextSize(text,font,fontScale=font_scale,thickness=1)[0]
text_offset_x=10
text_offset_y=img.shape[0] - 25

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(1)
if not cap.isOpened():
    cap=cv2.VideoCapture(0)
if not cap.isOpened():
        raise IOError("Cannot Open WebCam")

while True:
    ret,frame=cap.read()
    faceCascade=cv2.CascadeClassifier(cv2.data.haarcascades+ 'haarcascade_frontalface_default.xml')
    gray=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
    faces=faceCascade.detectMultiScale(gray,1.1,4)
    for x,y,w,h in facess:
        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)#bgr
        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]
    
    final_image=cv2.resize(face_roi,(224,224))
    final_image=np.expand_dims(final_image,axis=0)
    final_image=final_image/255.0

    font=cv2.FONT_HERSHEY_SIMPLEX

    Predictions=new_model.predict(final_image)

    font_scale=1.5
    font=cv2.FONT_HERSHEY_PLAIN

if(np.argmax(Predictions)==0):
        status="Angry"
        x1,y1,w1,h1=0,0,175,75
        cv2.rectangle(frame,(x1,x1),(x1 + w1,y1 + h1),(0,0,0),-1)
        cv2.putText(frame,status,(x1 +int(w1/10),y1 + int(h1/2)),cv2.FONT_HERSHEY_SIMPLEX,0.7,(0,0,255),2)
        cv2.putText(frame,status,(100,150),font,3,(0,0,255),2,cv2.LINE_4)
        cv2.rectangle(frame,(x,y),(x+w, y+h),(0,0,255))
        
elif (np.argmax(Predictions)==1):
        status="Disgust"
        x1,y1,w1,h1=0,0,175,75
        cv2.rectangle(frame,(x1,x1),(x1 + w1,y1 + h1),(0,0,0),-1)
        cv2.putText(frame,status,(x1 +int(w1/10),y1 + int(h1/2)),cv2.FONT_HERSHEY_SIMPLEX,0.7,(0,0,255),2)
        cv2.putText(frame,status,(100,150),font,3,(0,0,255),2,cv2.LINE_4)
        cv2.rectangle(frame,(x,y),(x+w, y+h),(0,0,255))
        
elif (np.argmax(Predictions)==2):
        status="Fear"
        x1,y1,w1,h1=0,0,175,75
        cv2.rectangle(frame,(x1,x1),(x1 + w1,y1 + h1),(0,0,0),-1)
        cv2.putText(frame,status,(x1 +int(w1/10),y1 + int(h1/2)),cv2.FONT_HERSHEY_SIMPLEX,0.7,(0,0,255),2)
        cv2.putText(frame,status,(100,150),font,3,(0,0,255),2,cv2.LINE_4)
        cv2.rectangle(frame,(x,y),(x+w, y+h),(0,0,255))
        
elif (np.argmax(Predictions)==3):
        status="Happy"
        x1,y1,w1,h1=0,0,175,75
        cv2.rectangle(frame,(x1,x1),(x1 + w1,y1 + h1),(0,0,0),-1)
        cv2.putText(frame,status,(x1 +int(w1/10),y1 + int(h1/2)),cv2.FONT_HERSHEY_SIMPLEX,0.7,(0,0,255),2)
        cv2.putText(frame,status,(100,150),font,3,(0,0,255),2,cv2.LINE_4)
        cv2.rectangle(frame,(x,y),(x+w, y+h),(0,0,255))
        
elif (np.argmax(Predictions)==4):
        status="Sad"
        x1,y1,w1,h1=0,0,175,75
        cv2.rectangle(frame,(x1,x1),(x1 + w1,y1 + h1),(0,0,0),-1)
        cv2.putText(frame,status,(x1 +int(w1/10),y1 + int(h1/2)),cv2.FONT_HERSHEY_SIMPLEX,0.7,(0,0,255),2)
        cv2.putText(frame,status,(100,150),font,3,(0,0,255),2,cv2.LINE_4)
        cv2.rectangle(frame,(x,y),(x+w, y+h),(0,0,255))
        
        
elif (np.argmax(Predictions)==5):
        status="Surprise"
        x1,y1,w1,h1=0,0,175,75
        cv2.rectangle(frame,(x1,x1),(x1 + w1,y1 + h1),(0,0,0),-1)
        cv2.putText(frame,status,(x1 +int(w1/10),y1 + int(h1/2)),cv2.FONT_HERSHEY_SIMPLEX,0.7,(0,0,255),2)
        cv2.putText(frame,status,(100,150),font,3,(0,0,255),2,cv2.LINE_4)
        cv2.rectangle(frame,(x,y),(x+w,y+h),(0,0,255))
else:
        status="Neutral"
        x1,y1,w1,h1=0,0,175,75
        cv2.rectangle(frame,(x1,x1),(x1 + w1,y1 + h1),(0,0,0),-1)
        cv2.putText(frame,status,(x1 +int(w1/10),y1 + int(h1/2)),cv2.FONT_HERSHEY_SIMPLEX,0.7,(0,0,255),2)
        cv2.putText(frame,status,(100,150),font,3,(0,0,255),2,cv2.LINE_4)
        cv2.rectangle(frame,(x,y),(x+w, y+h),(0,0,255))
        
        
        cv2.imshow("face emotion Recognition", frame)
        if cv2.waitkey(2)& 0xFF ==ord('q'):
             break
cap.release()
cv2.destroyAllWindows()

谁能告诉我什么是错误,是我的系统错误,因为它的位慢或有一些错误的代码?只有网络摄像头的灯是发光的,但没有相机窗口。我检查了我的网络摄像头是正常工作时,我打开相机应用程序。

q35jwt9p

q35jwt9p1#

变化
cv2.waitkey(2)cv2.waitKey(0)
waitKey中的K应为大写

k2fxgqgv

k2fxgqgv2#

尝试修改您的cv2.imshow('')...如果它有任何机会得到注解..这个错误发生...否则尝试使用else语句之外的代码段...也许这可以解决问题

相关问题