我的代码有什么问题,任何人都可以建议我

发布于 2025-01-17 08:28:56 字数 1816 浏览 0 评论 0 原文

我正在使用 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'

I'm working on real-time object detection using TensorFlow. So the first step is to collect images for some signs.

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()

This code is successfully running on Jupyter notebook but not in Google Colab
getting this error

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

顾冷 2025-01-24 08:28:56

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 call cv2_imshow(frame) to show your images.

https://colab.research.google.com/notebooks/snippets/advanced_outputs.ipynb#scrollTo=2viqYx97hPMi

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文