Google Colab:“使用网络摄像头进行实时视频流传输”

时间:2020-10-28 05:22:16

标签: machine-learning computer-vision video-streaming google-colaboratory cnn

我正在尝试学习如何使用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()”(在第二条代码的第二行)来运行模型吗?这是我的项目link。 在此先感谢:)

0 个答案:

没有答案