【发布时间】:2021-04-25 03:40:25
【问题描述】:
我写了以下代码
import cv2
import datetime
import time
import pandas as pd
cascPath = 'haarcascade_frontalface_dataset.xml' # dataset
faceCascade = cv2.CascadeClassifier(cascPath)
video_capture = cv2.VideoCapture('video1.mp4')
frames = video_capture.get(cv2.CAP_PROP_FRAME_COUNT)
fps = int(video_capture.get(cv2.CAP_PROP_FPS))
print(frames) #1403 frames
print(fps) #30 fps
# calculate duration of the video
seconds = int(frames / fps)
print("duration in seconds:", seconds) #46 seconds
df = pd.DataFrame(columns=['Time(Seconds)', 'Status'])
start = time.time()
print(start)
n=5
while True:
ret, frame = video_capture.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) #converts frame to grayscale image
faces = faceCascade.detectMultiScale(
gray, scaleFactor=1.1,
minNeighbors=5,
minSize=(30, 30),
flags=cv2.FONT_HERSHEY_SIMPLEX
)
if len(faces) == 0:
print(time.time()-start, 'No Face Detected')
df = df.append({'Time(Seconds)': (time.time()-start) , 'Status':'No Face detected' }, ignore_index=True)
else:
print(time.time()-start, 'Face Detected')
df = df.append({'Time(Seconds)':(time.time()-start), 'Status':'Face Detected' }, ignore_index=True)
# 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('Video', frame)
df.to_csv('output.csv', index = False)
if cv2.waitKey(1) & 0xFF == ord('q'):
# print(df.head(2))
break
# When everything is done, release the capture
video_capture.release()
cv2.destroyAllWindows()
如果你想下载我正在制作的视频,可以从here下载
从here下载haar cascade XML文件
我对此有一些疑问。
- 目前它在视频的所有 1403 帧上运行,我想对其进行优化,使其在每个
n帧之后运行推理,这是可定制的。在代码中我提到了 n = 5。因此,如果 n = 5。帧数应为 1403/5 = 280 - CSV 中的时间戳不准确,我希望它们与视频相关。基本上,第一列(时间(秒)应指定视频中的时间,状态应确定当时帧的状态(检测到/未检测到),时间(秒)列应在 46 秒左右结束,这是视频的长度。
- 我的 cv2.imshow 正在显示一个大约 2 倍速度的视频,我相信我可以通过使用 cv2.imKey() 来控制速度,cv2.waitKey 的最佳参数应该是什么,这样我才能获得类似的速度视频作为输出。
感谢您回答整个问题
【问题讨论】:
标签: python opencv machine-learning image-processing computer-vision