gpt4 book ai didi

python - OpenCV 升级网络摄像头的提要而不是使用全分辨率

转载 作者:太空宇宙 更新时间:2023-11-03 11:58:14 27 4
gpt4 key购买 nike

我有一个 1080p 网络摄像头 (Logitech v-u0032),我正在创建程序,每隔 X 秒从它的提要中保存一次静止图像,但是图像的侧面有黑条,分辨率似乎低于使用 Windows 内置相机应用程序拍摄的图像, 两个具有 1920x1080 像素但实际分辨率差异非常明显的输出文件

我一直在寻找改变分辨率的方法,但 OpenCV 似乎总是升级到新的分辨率

import cv2
import numpy as np
import datetime
import time
import os

cv2.namedWindow("Unregistered Hypercam")
vc = cv2.VideoCapture(0)

vc.set(3, 1920) # Set the horizontal resolution
vc.set(4, 1080) # Set the vertical resolution
vc.set(5, 10) # Set the framerate

if vc.isOpened():
rval, frame = vc.read()
else:
rval = False

lastSave = time.time()

def mfold(name):
try:
os.mkdir(name)
except OSError:
x = 0
else:
print ("Successfully created the directory %s " % fold)

while rval:
cv2.imshow("Unregistered Hypercam", frame)
rval, frame = vc.read()
now = datetime.datetime.now()

if time.time() > lastSave + 10:
lastSave = time.time()
fold = '\\snapshots\\' # This will create folder C:\snapshots
mfold(fold)
cv2.imwrite(fold + str(now.year) + '.' + str(now.month) + '.' + str(now.day) + ' ' + str(now.hour) + 'h_' + str(now.minute) + 'm_' + str(now.second) + 's.png', frame)

key = cv2.waitKey(20)
if key == 27:
break

cv2.destroyWindow("Unregistered Hypercam")

图像模糊且不清晰,带有黑条,与使用 Windows 相机应用程序拍摄的图像完全不同

最佳答案

这是一个示例小部件,用于将网络摄像头/流保存到视频文件或屏幕截图图像中。它保持流的原始分辨率。我使用了我的一个 IP 摄像机流而不是网络摄像头,但它应该与网络摄像头一样工作。目前它打开一个流并将其保存为视频,但您可以修改它以定期截取屏幕截图。此外,要修改分辨率,您可以使用此功能在保持宽高比的同时手动调整框架大小(目前在小部件中未使用)。

在保持宽高比的同时调整帧的分辨率

# Resizes a image and maintains aspect ratio
def maintain_aspect_ratio_resize(image, width=None, height=None, inter=cv2.INTER_AREA):
# Grab the image size and initialize dimensions
dim = None
(h, w) = image.shape[:2]

# Return original image if no need to resize
if width is None and height is None:
return image

# We are resizing height if width is none
if width is None:
# Calculate the ratio of the height and construct the dimensions
r = height / float(h)
dim = (int(w * r), height)
# We are resizing width if height is none
else:
# Calculate the ratio of the 0idth and construct the dimensions
r = width / float(w)
dim = (width, int(h * r))

# Return the resized image
return cv2.resize(image, dim, interpolation=inter)

你可以像这样使用它来调整宽度或高度

rval, frame = vc.read()

resize_width = maintain_aspect_ratio_resize(frame, width=500)
resize_height = maintain_aspect_ratio_resize(frame, height=500)

流式传输和保存视频小部件

from threading import Thread
import cv2

class VideoWriterObject(object):
def __init__(self, src=0):
# Create a VideoCapture object
self.capture = cv2.VideoCapture(src)

# Default resolutions of the frame are obtained (system dependent)
self.frame_width = int(self.capture.get(3))
self.frame_height = int(self.capture.get(4))

# Set up codec and output video settings
self.codec = cv2.VideoWriter_fourcc('M','J','P','G')
self.output_video = cv2.VideoWriter('output.avi', self.codec, 30, (self.frame_width, self.frame_height))

# Start the thread to read frames from the video stream
self.thread = Thread(target=self.update, args=())
self.thread.daemon = True
self.thread.start()

def update(self):
# Read the next frame from the stream in a different thread
while True:
if self.capture.isOpened():
(self.status, self.frame) = self.capture.read()

def show_frame(self):
# Display frames in main program
if self.status:
cv2.imshow('frame', self.frame)

# Press Q on keyboard to stop recording
key = cv2.waitKey(1)
if key == ord('q'):
self.capture.release()
self.output_video.release()
cv2.destroyAllWindows()
exit(1)

def save_frame(self):
# Save obtained frame into video output file
self.output_video.write(self.frame)

if __name__ == '__main__':
video_stream_widget = VideoWriterObject(0)
while True:
try:
video_stream_widget.show_frame()
video_stream_widget.save_frame()
except AttributeError:
pass

关于python - OpenCV 升级网络摄像头的提要而不是使用全分辨率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56556492/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com