gpt4 book ai didi

Python OpenCV - 在根据特定对象条件保存视频时,并非所有此类帧都会被保存

转载 作者:太空宇宙 更新时间:2023-11-03 20:00:59 25 4
gpt4 key购买 nike

我在Python中使用opencv,并尝试仅在帧中存在特定类型的对象/标签(例如“伞”)时记录/保存视频中的那些帧

问题:

它正确地从第一次在帧中找到提到的对象/标签的实例开始保存帧,但如果该对象/标签在接下来的几帧中不存在并且仅在几帧后出现,那么这些帧不会被保存到我正在保存的 mp4 文件。

它只保存提到的对象的第一个连续帧,不保存后面的帧。

阅读此链接的建议后,我通过将框架编写步骤放入 for 循环中来编辑代码,如下所示: OpenCV - Save video segments based on certion condition

框架编写我尝试即兴创作的代码

# saving video frame by frame             
for frame_numb in range(total_frames):
if i == '':
pass
else:
if "umbrella" in label:
print("umbrella in labels")

# Issue causing part where I may need some change
out_vid.write(frame[frame_numb])

上述代码更改的结果:

它仅创建 256kb 文件,并且文件无法打开/未写入任何内容

如果我对代码进行以下更改,那么它只会保存满足该条件的视频的第一帧,并在整个时间内运行同一帧

    # saving video frame by frame             
for frame_numb in range(total_frames):
if i == '':
pass
else:
if "umbrella" in label:
print("umbrella in labels")

# Issue causing part where I may need some change
out_vid.write(frame)

分享下面更大的代码块以供引用:

def vid_objects_detection(type=0, confidence_threshold=0.5, image_quality=416):

classes = []

# reading category names from coco text file and inserting in classes list
with open("coco.names", "r") as f:
classes = [line.strip() for line in f.readlines()]

net = cv2.dnn.readNet("yolov3-tiny.weights", "yolov3-tiny.cfg") # using tiny versions of weights & config file

layer_names = net.getLayerNames()
output_layers = [layer_names[i[0] - 1] for i in net.getUnconnectedOutLayers()]

# Loading video
cap = cv2.VideoCapture(type) # use 0 for webcam

_, frame = cap.read()
height, width, channels = frame.shape

# providing codec for writing frames to video
fourcc = cv2.VideoWriter_fourcc(*'MP4V')

# Write video with name & size. Should be of same size(width, height) as original video
out_vid = cv2.VideoWriter('obj_detect4_'+str(type), fourcc, 20.0, (width,height))

font = cv2.FONT_HERSHEY_COMPLEX_SMALL
starting_time = time.time()
frame_id = 0

while True:
_, frame = cap.read()

frame_id +=1

total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
height, width, channels = frame.shape

blob = cv2.dnn.blobFromImage(frame, 0.00392, (image_quality, image_quality), (0, 0, 0), True, crop=False)
net.setInput(blob)

outs = net.forward(output_layers)

# For showing informations on screen
class_ids = []
confidences = []
boxes = []
for out in outs:
for detection in out:
# claculated scores, class_id, confidence

if confidence > confidence_threshold:
# claculatedd center_x, center_y, w,h,x,y
boxes.append([x, y, w, h])
confidences.append(float(confidence))
class_ids.append(class_id)
print("confidences:", confidences)
print(class_ids)
print("boxes", boxes)

indexes = cv2.dnn.NMSBoxes(boxes, confidences, confidence_threshold, 0.4)

for i in range(len(boxes)):
if i in indexes:
x, y, w, h = boxes[i]
label = str(classes[class_ids[i]])

elapsed_time = time.time() - starting_time
fps = frame_id / elapsed_time
time_display = time.strftime("%a, %d%b%Y %H:%M:%S", time.localtime())
cv2.putText(frame,"|FPS: " + str(round(fps,3)), (10, 40), font, 1, (0,255,0), 1)
print(fps)

# saving video frame by frame
if i == '':
pass
else:
if 'umbrella' in label:
out_vid.write(frame)

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

cap.release()
out_vid.release()
cv2.destroyAllWindows()

# calling function
vid_objects_detection("walking.mp4")

我在代码中修剪了一些小计算并插入了注释,以减少代码的长度

最佳答案

有时视频编解码器会执行所谓的关键帧压缩。这意味着,一帧被完全存储,例如每 10 帧,而其间的所有其他帧都被存储为更改或增量。在这些情况下,当您尝试仅保存这些中间帧时,它们可能不会被保存。但在这些情况下,如果您按顺序迭代每个帧,则保存帧是有效的。

也许您可以注释掉 out_vid = cv2.VideoWriter('obj_detect4_'+str(type), fourcc, 20.0, (width,height)) 行,然后尝试保存帧根据您的情况从网络摄像头流中获取。

关于Python OpenCV - 在根据特定对象条件保存视频时,并非所有此类帧都会被保存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59237959/

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