想在框架中存在脸时捕获所有框架

发布于 2025-02-11 11:34:27 字数 689 浏览 1 评论 0原文

我正在尝试构建面部图像的数据集,我已经搜索并找到了一个与笔记本电脑的网络摄像头正常工作的代码,但是当我尝试使用IP摄像机捕获通常用于监视的IP相机时,而不是代码不起作用。

import cv2
import numpy as np

face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_alt2.xml')

cap = cv2.VideoCapture(0)

count = 0
while True:
    ret,frame = cap.read()
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    faces = face_cascade.detectMultiScale(gray, 1.3, 5)
    for (x, y, w, h) in faces:
        cv2.imwrite("frame%d.jpg" % count, frame)
        count = count + 1
    if cv2.waitKey(2) & 0xFF == ord('q'):
        break
cap.release()
cv2.destroyAllWindows()

我想提到的另一件事是我的笔记本电脑摄像头分辨率是640.0 480.0,IP摄像机具有2560.0 1440.0。请帮助我在脸上捕获所有框架。

I am trying to build a dataset of face images, I have searched and find a code that is working fine with my laptop's webcam but when I am trying to capture with IP Camera which is usually used for surveillance than code is not working.

import cv2
import numpy as np

face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_alt2.xml')

cap = cv2.VideoCapture(0)

count = 0
while True:
    ret,frame = cap.read()
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    faces = face_cascade.detectMultiScale(gray, 1.3, 5)
    for (x, y, w, h) in faces:
        cv2.imwrite("frame%d.jpg" % count, frame)
        count = count + 1
    if cv2.waitKey(2) & 0xFF == ord('q'):
        break
cap.release()
cv2.destroyAllWindows()

Another thing I want to mention my laptop Camera resolution is 640.0 480.0 and IP Cameras have 2560.0 1440.0. Please help me to capture all frames when face is present.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

一杆小烟枪 2025-02-18 11:34:27

要从特定摄像机捕获图像,您需要在cv2.VIDEOCAPTURE(参数)中更改参数。例如:

  • cv2.VIDEOCAPTURE(0):用于选择默认摄像机,通常是笔记本电脑内置摄像机。
  • cv2.videocapture(n) 其中n> = 1:它用于通过USB/其他端口选择其他外部摄像机连接到笔记本电脑
  • cv2.videocapture('协议:// ip:port/1'):它用于从网络摄像机(如您的情况下)获取
  • 协议表示:http/rtsp等。
  • 表示托管地址:示例:192.168.18.37
  • port在这种情况下访问IP地址的位置

应为:

cv2.VideoCapture('rtsp://admin:[email protected]/H264?ch=2&subtype=0/1')

To capture images from specific camera you need to change arguments in cv2.VideoCapture(argument). For example:

  • cv2.VideoCapture(0): it's used to select the default camera, generally laptop inbuilt camera.
  • cv2.VideoCapture(n) where n>=1: it's used to select the other external camera connect to laptop through usb/other ports
  • cv2.VideoCapture('protocol://IP:port/1'): it's used to take input from network cameras(like in your case)
  • Here protocol means: HTTP/RTSP etc.
  • IP means hosting address: Example: 192.168.18.37
  • port where the IP address is accessed

In this case it should be:

cv2.VideoCapture('rtsp://admin:[email protected]/H264?ch=2&subtype=0/1')
夕色琉璃 2025-02-18 11:34:27

在调整分辨率后,问题已经解决,我添加了框架调整的代码行frair = cv2.Resize(frame,(1360,768)) 比代码开始正常工作。

此代码可能对任何人有帮助。

import cv2
import numpy as np

face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_alt2.xml')

cap = cv2.VideoCapture('rtsp://admin:[email protected]/H264?ch=2&subtype=0/1')

count = 0
while True:
    ret,frame = cap.read()
    frame = cv2.resize(frame, (1360, 768))
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    faces = face_cascade.detectMultiScale(gray, 1.3, 5)
    for (x, y, w, h) in faces:
        cv2.imwrite("frame%d.jpg" % count, frame)
        count = count + 1
    if cv2.waitKey(2) & 0xFF == ord('q'):
        break
cap.release()
cv2.destroyAllWindows()

The issue has been resolved after adjusting the resolution, I have added the line of code for frame adjustment frame = cv2.resize(frame, (1360, 768)) than code has started working properly.

This code might be helpful for anyone.

import cv2
import numpy as np

face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_alt2.xml')

cap = cv2.VideoCapture('rtsp://admin:[email protected]/H264?ch=2&subtype=0/1')

count = 0
while True:
    ret,frame = cap.read()
    frame = cv2.resize(frame, (1360, 768))
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    faces = face_cascade.detectMultiScale(gray, 1.3, 5)
    for (x, y, w, h) in faces:
        cv2.imwrite("frame%d.jpg" % count, frame)
        count = count + 1
    if cv2.waitKey(2) & 0xFF == ord('q'):
        break
cap.release()
cv2.destroyAllWindows()
一个人的旅程 2025-02-18 11:34:27

在图像捕获之前更改分辨率更为最佳。无需调整后续帧大小。

Before the while loop set the width and height as follows:

cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 768)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1380)

Changing the resolution before image capture is more optimal. There would be no need to resize every subsequent frame.

Before the while loop set the width and height as follows:

cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 768)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1380)

Different flags used for video streaming

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