machine-learning - 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()”(在我的第二个代码的第二行)来运行模型?这是我的项目链接。提前致谢 :)
解决方案
推荐阅读
- html - Google Script Web App:无法在“DOMWindow”上执行“postMessage”:提供的目标原点与接收窗口的原点不匹配
- bash - 如何对齐列文件中的内容
- python - 在curses中显示带有尾随空格的文本
- php - 如何将随机路由(404)定向到laravel中的一个视图页面?
- r - 按列名划分每个单元格
- python-3.x - 我正在循环一个数据框pypandas。我想计算特定列中的当前值
- python - 如何从没有索引的列中获取数据
- elasticsearch - 是否可以免费使用 elasticsearch、lostash 和 Kibana 来创建我自己的产品并销售?
- node.js - Redis 有时需要很长时间才能加载数据
- algorithmic-trading - 如何在 Tradingview Pine Script v4 策略中设置追踪止损?