【问题标题】:webcam image distorted in python网络摄像头图像在python中失真
【发布时间】:2020-08-13 05:10:37
【问题描述】:
import cv2
import sys

faceCascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')

video_capture = cv2.VideoCapture(0)

img_counter = 0

while True:
    # Capture frame-by-frame
    ret, frame = video_capture.read()

    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    k = cv2.waitKey(1)
    faces = faceCascade.detectMultiScale(
        gray,
        scaleFactor=1.5,
        minNeighbors=5,
        minSize=(30, 30),
        flags=cv2.CASCADE_SCALE_IMAGE
    )

    # Draw a rectangle around the faces
    for (x, y, w, h) in faces:
        cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)

    # Display the resulting frame
    cv2.imshow('FaceDetection', frame)

    if k%256 == 27: #ESC Pressed
        break
    elif k%256 == 32:
        # SPACE pressed
        img_name = "facedetect_webcam_{}.png".format(img_counter)
        cv2.imwrite(img_name, frame)
        print("{} written!".format(img_name))
        img_counter += 1


# When everything is done, release the capture
video_capture.release()
cv2.destroyAllWindows()

after running the code in python the image of my webcam turn into this

【问题讨论】:

  • 你能减少你的代码,看看在捕获或颜色转换后是否已经直接出现失真?
  • 我尝试使用其他简单的代码,但网络摄像头流是相同的。
  • 你能发布已经导致问题的最小代码吗?
  • 在此之前找到的代码工作,可能是我安装的一些库和其他软件导致了这个问题,我安装了Cmake和其他库dlib,opencv人脸识别,opencv contrib python和其他
  • 我遇到了同样的问题,这个解决方案虽然是针对不同的问题,但对我有用video_capture = cv2.VideoCapture(cv2.CAP_DSHOW)取自这个stackoverflow答案link

标签: python opencv


【解决方案1】:

您的代码对我有效,没有任何问题。
输出图像看起来输入图像的数据类型有问题。

这里讨论过类似的东西:
https://answers.opencv.org/question/174027/c-ycbcr422-to-rgb-convert-from-raw-data-file/

您能否也发布您的输入帧灰度图像?
你用的是什么相机?

尝试在 video_capture.read() 之后添加此代码并发布输出。

print(frame.dtype)
print(frame.shape)

我的是:dtype - uint8,shape - (480, 640, 3)。
您可能会遇到通道数(例如 RGBA)和矩形函数的问题。

这些只是我对可能出错的想法。但对我来说,你的代码运行良好。

br约瑟夫

【讨论】:

  • uint8 (480, 640, 3),代码在此之前实际工作 find 。可能是我安装的其他库或软件。我尝试测试我的网络摄像头,发现它可以工作
猜你喜欢
  • 1970-01-01
  • 2020-07-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多