首页 > 解决方案 > Google Colab:“使用网络摄像头直播视频流”

问题描述

我正在尝试学习如何使用 Google Colab 在实时视频中检测面具。虽然我可以使用相机的代码来捕捉图像。但我无法流式传输实时视频。怎么可能呢?这是我的项目的示例代码。它是在本地计算机上使用 VideoCapture(0) 完成的。VideoCapture 返回值和图像类型。但是当我在 colab 中使用 take_photo() 的代码时,它返回的结果与我的第二个代码的第二行中的 VideoCapture 函数不同。这就是为什么我无法得到结果。

 face_clsfr = cv2.CascadeClassifier('/content/drive/My Drive/Colab Notebooks/dataset/face mask detection/face detection.xml') #path of facedetection xml file
    source = cv2.VideoCapture(0) #video capture on camera 0 
    labels_dict = {0: 'Mask !!!' , 1: ' No Mask !!!'}
    color_dict = {0:(0,255,0) , 1:(255,0,0)} # rgb color 

while(True):

    ret,img=source.read()
    gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    faces=face_clsfr.detectMultiScale(gray,1.3,5)  

    for (x,y,w,h) in faces:
    
        face_img=gray[y:y+w,x:x+w]
        resized=cv2.resize(face_img,(100,100))
        normalized=resized/255.0
        reshaped=np.reshape(normalized,(1,100,100,1))
        result=model.predict(reshaped)

        label=np.argmax(result,axis=1)[0]
      
        cv2.rectangle(img,(x,y),(x+w,y+h),color_dict[label],2)
        cv2.rectangle(img,(x,y-40),(x+w,y),color_dict[label],-1)
        cv2.putText(img, labels_dict[label], (x, y-10),cv2.FONT_HERSHEY_SIMPLEX,0.8,(255,255,255),2)
        
        
    cv2.imshow('LIVE',img)
    key=cv2.waitKey(1)
    
    if(key==27):
        break
        
cv2.destroyAllWindows()
source.release()

我怎么解决这个问题?如何使用实时视频流在 colab 中运行这个模型,我需要什么来替换这个“ret,img=source.read()”(在我的第二个代码的第二行)来运行模型?这是我的项目链接。提前致谢 :)

标签: machine-learningcomputer-visionvideo-streaminggoogle-colaboratoryconv-neural-network

解决方案


推荐阅读