从网络摄像头拍摄图像

Hamzah Khan |

我有此脚本从网络摄像头捕获图像,但是它没有保存任何图像,我不知道为什么。我收到此错误“ Traceback(最近一次通话):文件“ C:/Users/Iram/.PyCharmCE2019.3/config/scratches/scratch_5.py”,第26行,灰色= cv2.cvtColor(img_,cv2 .COLOR_BGR2GRAY)cv2。错误:OpenCV(4.2.0)C:\ projects \ opencv-python \ opencv \ modules \ imgproc \ src \ color.cpp:182:错误:(-215:断言失败)!_src.empty( )在函数'cv :: cvtColor'[WARN:0]全局C:\ projects \ opencv-python \ opencv \ modules \ videoio \ src \ cap_msmf.cpp(674)SourceReaderCB :: ~~ SourceReaderCB终止异步回调

我的代码是

import cv2
import datetime
i = 1
key = cv2.waitKey(1)
webcam = cv2.VideoCapture(0)
while True:
    try:
        check, frame = webcam.read()
        print(check)  # prints true as long as the webcam is running
        print(frame)  # prints matrix values of each framecd
        cv2.imshow("Capturing", frame)
        key = cv2.waitKey(1)
        if key == ord('s'):
            cv2.imwrite(filename='saved_img.jpg,%Y-%b-%d %H:%M:%S', img=frame)
            #print('Timestamp: {:%Y-%b-%d %H:%M:%S}'.format(datetime.datetime.now()))
            i += 1
            print('%i')

            # webcam.release()
            img_new = cv2.imread('saved_img.jpg,%Y-%b-%d %H:%M:%S', cv2.IMREAD_GRAYSCALE)
            # cv2.imshow("Captured Image", img_new)
            # cv2.waitKey(1925)
            print("Processing image...")
            img_ = cv2.imread('saved_img.jpg,%Y-%b-%d %H:%M:%S', cv2.IMREAD_ANYCOLOR)
            print("Converting RGB image to grayscale...")
            gray = cv2.cvtColor(img_, cv2.COLOR_BGR2GRAY)
            print("Converted RGB image to grayscale...")
            print("Resizing image to 28x28 scale...")
            img_ = cv2.resize(gray, (28, 28))
            print("Resized...")
            img_resized = cv2.imwrite(filename='saved_img-final.jpg', img=img_)
            print("Image saved!")

        elif key == ord('q'):
            print("Turning off camera.")
            webcam.release()
            print("Camera off.")
            print("Program ended.")
            cv2.destroyAllWindows()
            break

    except(KeyboardInterrupt):
        print("Turning off camera.")
        webcam.release()
        print("Camera off.")
        print("Program ended.")
        cv2.destroyAllWindows()
        camera.release()

        break
        camera.release()
        cv2.destroyAllWindows()

有人可以帮我吗?

错误的根源是您使用的文件名带有冒号 :

cv2.imwrite(filename='saved_img.jpg,%Y-%b-%d %H:%M:%S', img=frame)

包含冒号的文件名是非法的。

  • 图像(框架)未写入文件。
  • 读取图像img_ = cv2.imread('saved_img.jpg,%Y-%b-%d %H:%M:%S', cv2.IMREAD_ANYCOLOR)返回None
  • 传递Nonecv2.cvtColor引发OpenCV异常。

建议的解决方法:

  • 使用有效的文件名,例如:file_name = datetime.datetime.now().strftime('%Y-%m-%d_%H-%M- %S.jpg')
  • file_name读写时使用变量
  • 我还建议以验证checkTrue使用前frame

我创建了一个示例代码进行测试。
该代码生成合成视频文件,并从生成的文件而不是从摄像机读取帧。

这是测试代码:

import numpy as np
import cv2
import datetime

intput_filename = 'input_vid.avi'

# Generate synthetic video files to be used as input:
###############################################################################
width, height, n_frames = 640, 480, 100  # 100 frames, resolution 640x480

# Use motion JPEG codec (for testing)
synthetic_out = cv2.VideoWriter(intput_filename, cv2.VideoWriter_fourcc(*'MJPG'), 25, (width, height))

for i in range(n_frames):
    img = np.full((height, width, 3), 60, np.uint8)
    cv2.putText(img, str(i+1), (width//2-100*len(str(i+1)), height//2+100), cv2.FONT_HERSHEY_DUPLEX, 10, (30, 255, 30), 20)  # Green number
    synthetic_out.write(img)

synthetic_out.release()
###############################################################################


i = 1
key = cv2.waitKey(1)

# Read video from file instead of from camera (for testing)
webcam = cv2.VideoCapture(intput_filename)

while True:
    try:
        check, frame = webcam.read()
        print(check)  # prints true as long as the webcam is running

        if check:
            print(frame)  # prints matrix values of each frame
            cv2.imshow("Capturing", frame)

            if key == ord('s'):
                file_name = datetime.datetime.now().strftime('%Y-%m-%d_%H-%M-%S.jpg')

                cv2.imwrite(file_name, img=frame)
                #print('Timestamp: {:%Y-%b-%d %H:%M:%S}'.format(datetime.datetime.now()))
                i += 1
                print(str(i))

                # webcam.release()
                img_new = cv2.imread(file_name, cv2.IMREAD_GRAYSCALE)
                # cv2.imshow("Captured Image", img_new)
                # cv2.waitKey(1925)
                print("Processing image...")
                img_ = cv2.imread(file_name, cv2.IMREAD_ANYCOLOR)
                print("Converting RGB image to grayscale...")
                gray = cv2.cvtColor(img_, cv2.COLOR_BGR2GRAY)
                print("Converted RGB image to grayscale...")
                print("Resizing image to 28x28 scale...")
                img_ = cv2.resize(gray, (28, 28))
                print("Resized...")
                img_resized = cv2.imwrite(filename='saved_img-final.jpg', img=img_)
                print("Image saved!")

        #key = cv2.waitKey(1)
        key = cv2.waitKey(100)  # Wait 100 msec (just for testing).

        if key == ord('q'):
            print("Turning off camera.")
            print("Camera off.")
            print("Program ended.")
            break

    except(KeyboardInterrupt):
        print("Turning off camera.")
        print("Camera off.")
        print("Program ended.")
        break

webcam.release()
cv2.destroyAllWindows()

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

为什么用Python拍摄的网络摄像头图像如此暗?

来自分类Dev

查询使网络摄像头每x秒拍摄一次图像的脚本

来自分类Dev

上传到服务器时,从网络摄像头拍摄的上传图像已损坏

来自分类Dev

从网址/网络摄像头保存图像

来自分类Dev

延迟从网络摄像头拍摄单个快照

来自分类Dev

我的网络摄像头拍摄的照片与预览的照片不同

来自分类Dev

OpenCV Python 在拍摄之前查看网络摄像头馈送?

来自分类Dev

网络摄像头图像与打印图像不同

来自分类Dev

JavaScript,用于网络摄像头图像的PHP Array帮助,

来自分类Dev

jQuery网络摄像头插件未发布捕获的图像

来自分类Dev

如何使用DirectShow和网络摄像头预览图像

来自分类Dev

通过网络摄像头检测图像中的文本

来自分类Dev

如何在Skype上调整网络摄像头图像?

来自分类Dev

Python | 按时间间隔从网络摄像头捕获图像

来自分类Dev

如何在Skype上调整网络摄像头图像?

来自分类Dev

将图像从网络摄像头转换为灰度OpenCV

来自分类Dev

禁用网络摄像头图像捕获设置弹出窗口

来自分类Dev

jQuery网络摄像头插件未发布捕获的图像

来自分类Dev

使用Opencv直接从网络摄像头获取灰度图像

来自分类Dev

OpenCV使过饱和的网络摄像头图像变暗

来自分类Dev

镜像网络摄像头图像的右半部分

来自分类Dev

在Python中从网络摄像头捕获图像

来自分类Dev

Java渲染网络摄像头的图像占用太多CPU

来自分类Dev

奇怪的动画猫代替了网络摄像头图像

来自分类Dev

无法在OpenCV中使用网络摄像头显示图像

来自分类Dev

PHP + Ajax,从网络摄像头获取新鲜图像

来自分类Dev

在C#中从网络摄像头捕获图像

来自分类Dev

启动网络摄像头并使用python捕获图像

来自分类Dev

Python dlib - 读取图像而不是网络摄像头

Related 相关文章

  1. 1

    为什么用Python拍摄的网络摄像头图像如此暗?

  2. 2

    查询使网络摄像头每x秒拍摄一次图像的脚本

  3. 3

    上传到服务器时,从网络摄像头拍摄的上传图像已损坏

  4. 4

    从网址/网络摄像头保存图像

  5. 5

    延迟从网络摄像头拍摄单个快照

  6. 6

    我的网络摄像头拍摄的照片与预览的照片不同

  7. 7

    OpenCV Python 在拍摄之前查看网络摄像头馈送?

  8. 8

    网络摄像头图像与打印图像不同

  9. 9

    JavaScript,用于网络摄像头图像的PHP Array帮助,

  10. 10

    jQuery网络摄像头插件未发布捕获的图像

  11. 11

    如何使用DirectShow和网络摄像头预览图像

  12. 12

    通过网络摄像头检测图像中的文本

  13. 13

    如何在Skype上调整网络摄像头图像?

  14. 14

    Python | 按时间间隔从网络摄像头捕获图像

  15. 15

    如何在Skype上调整网络摄像头图像?

  16. 16

    将图像从网络摄像头转换为灰度OpenCV

  17. 17

    禁用网络摄像头图像捕获设置弹出窗口

  18. 18

    jQuery网络摄像头插件未发布捕获的图像

  19. 19

    使用Opencv直接从网络摄像头获取灰度图像

  20. 20

    OpenCV使过饱和的网络摄像头图像变暗

  21. 21

    镜像网络摄像头图像的右半部分

  22. 22

    在Python中从网络摄像头捕获图像

  23. 23

    Java渲染网络摄像头的图像占用太多CPU

  24. 24

    奇怪的动画猫代替了网络摄像头图像

  25. 25

    无法在OpenCV中使用网络摄像头显示图像

  26. 26

    PHP + Ajax,从网络摄像头获取新鲜图像

  27. 27

    在C#中从网络摄像头捕获图像

  28. 28

    启动网络摄像头并使用python捕获图像

  29. 29

    Python dlib - 读取图像而不是网络摄像头

热门标签

归档