我的代码有什么问题,任何人都可以建议我
我正在使用 TensorFlow 进行实时对象检测。所以第一步是收集一些标志的图像。
import cv2
import os
import time
import uuid
cv2.__version__
labels = ['hi']
number_imgs = 5
IMAGES_PATH = os.path.join('Tensorflow', 'workspace', 'images', 'collectedimages')
if not os.path.exists(IMAGES_PATH):
if os.name == 'posix':
!mkdir -p {IMAGES_PATH}
if os.name == 'nt':
!mkdir {IMAGES_PATH}
for label in labels:
path = os.path.join(IMAGES_PATH, label)
if not os.path.exists(path):
!mkdir {path}
for label in labels:
cap = cv2.VideoCapture(0)
print('Collecting images for {}'.format(label))
time.sleep(5)
for imgnum in range(number_imgs):
print('Collecting image {}'.format(imgnum))
ret, frame = cap.read()
imgname = os.path.join(IMAGES_PATH,label,label+'.'+'{}.jpg'.format(str(uuid.uuid1())))
cv2.imwrite(imgname, frame)
cv2.imshow('frame', frame)
time.sleep(2)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
此代码在 Jupyter 笔记本上成功运行,但在 Google Colab 中失败 出现此错误
Collecting images for hi Collecting image 0
--------------------------------------------------------------------------- error Traceback (most recent call last) <ipython-input-23-d176419936b4> in <module>()
7 ret, frame = cap.read()
8 imgname = os.path.join(IMAGES_PATH,label,label+'.'+'{}.jpg'.format(str(uuid.uuid1())))
----> 9 cv2.imwrite(imgname, frame)
10 cv2.imshow('frame', frame)
11 time.sleep(2)
error: OpenCV(4.1.2) /io/opencv/modules/imgcodecs/src/loadsave.cpp:715: error: (-215:Assertion failed) !_img.empty() in function 'imwrite'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Google Colab是基于浏览器的,因此无法访问没有API的网络摄像头等本地硬件。有一个示例代码可在下面访问计算机的网络摄像头。请注意,iMshow在Google Colab中也不可用,您需要从google.colab.patches导入cv2_imshow 并调用
cv2_imshow(frame)
以显示您的图像。Google colab is browser based, so there's no way to access local hardware such as webcams without and API. There is an example code for accessing your computer's webcam below. Please note that imshow is also not available in Google Colab, you need to execute
from google.colab.patches import cv2_imshow
and callcv2_imshow(frame)
to show your images.https://colab.research.google.com/notebooks/snippets/advanced_outputs.ipynb#scrollTo=2viqYx97hPMi