如何使用opencv python和Raspbery Pi 3在摄像头每5秒钟后捕获一张图片? [英] How to capture a picture after every 5 seconds of camera using opencv python and Raspbery Pi 3?

查看:193
本文介绍了如何使用opencv python和Raspbery Pi 3在摄像头每5秒钟后捕获一张图片?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从事一个学校项目,该项目与使用OpenCV Python和Raspberry Pi 3进行图像处理有关.

I am working on a school project which is related to Image Processing using OpenCV Python and Raspberry Pi 3.

Raspberry Pi 3的硬件无法连续处理来自摄像机的视频,因此我正在考虑仅每隔5秒钟从摄像机中捕获一张图片,并使用它来识别我需要的内容,然后继续.

The hardware of Raspberry Pi 3 cannot handle the video from camera consecutively, therefore I'm thinking about only capture a picture after every 5 seconds from the camera and use it to recognize what I need, then continue.

我在互联网上进行了一些研究,发现了一个名为time.sleep(5)的函数,但是该函数仅将相机暂停5秒钟,然后继续.

I did some research on the internet and found a function called time.sleep(5), however this function only pause the camera 5 seconds and then continue.

任何人都可以帮助我解决我的问题.太感谢了. 对不起,我的英语不好,这是到目前为止的代码.在我的代码中,我首先使用视频进行测试,然后找出解决方案后,将其应用在相机上.

Can anyone help me to solve my problem. Thank you so much. Sorry for my bad English, here is my code so far. In my code, I use a video to test first, then when figure out the solution, I will apply on camera.

import cv2
import numpy as np
import time

headcc = cv2.CascadeClassifier('lib/heads_cascade.xml')
cap = cv2.VideoCapture('image/hockey_game.avi')

def video():
    ret, frame = cap.read()

    # gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    head = headcc.detectMultiScale(frame, 1.2, 2, 0 , (20, 20), (40, 40))

    print type(head)
    print head
    print head.shape
    print "Number of heads detected: " + str(head.shape[0])


    if len(head) > 0:
        for (x, y, w, h) in head:
            cv2.rectangle(frame, (x,y), (x+w, y+h), (0, 255, 255), 1)

    cv2.rectangle(frame, ((0,frame.shape[0] -25)),(270, frame.shape[0]), (255,255,255), -1)
    cv2.putText(frame, "Number of head detected: " + str(head.shape[0]), (0,frame.shape[0] -10), cv2.FONT_HERSHEY_TRIPLEX, 0.5,  (0,0,0), 1)

    cv2.namedWindow('Camera',cv2.WINDOW_NORMAL)
    cv2.imshow('Camera',frame)


while(cap.isOpened()):
    video()
    time.sleep(5)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

更新2018年3月15日: 感谢乔的评论,我找到了跳过帧的方法.但是,程序仍然无法确定视频是否结束,并且fps代码仍显示为0.0.因此,如果对这些错误有任何帮助,我将不胜感激.这是到目前为止的代码:

Update March 15th, 2018: Thanks to comment from Joe, I have found the way to skip frames. However, the program still cannot know whether the video ends, and also the fps code still appear 0.0. So if there is any help for these errors, I will really appreciate. Here is the code so far:

import cv2
import numpy as np
import time

headcc = cv2.CascadeClassifier('lib/heads_cascade.xml')
cap = cv2.VideoCapture('image/hockey_game.avi')
fps = cap.get(cv2.CAP_PROP_POS_FRAMES)

def video():
    ret, frame = cap.read()

    # gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    head = headcc.detectMultiScale(frame, 1.2, 2, 0 , (20, 20), (40, 40))

    # print type(head)
    # print head
    # print head.shape
    print "Number of heads detected: " + str(head.shape[0])

    if len(head) > 0:
        for (x, y, w, h) in head:
            cv2.rectangle(frame, (x,y), (x+w, y+h), (0, 255, 255), 1)

    # cv2.rectangle(frame, ((0,frame.shape[0] -25)),(270, frame.shape[0]), (255,255,255), -1)
    # cv2.putText(frame, "Number of head detected: " + str(head.shape[0]), (0,frame.shape[0] -10), cv2.FONT_HERSHEY_TRIPLEX, 0.5,  (0,0,0), 1)

    cv2.namedWindow('Camera',cv2.WINDOW_NORMAL)
    cv2.imshow('Camera',frame)


while(cap.isOpened()):
    video()
    cf = cap.get(cv2.CAP_PROP_POS_FRAMES) - 1
    cap.set(cv2.CAP_PROP_POS_FRAMES, cf+50)
    # cv2.setTrackbarPos("pos_trackbar", "Frame Grabber", 
int(cap.get(cv2.CAP_PROP_FPS)))
    time.sleep(2)
    if (cv2.waitKey(1) & 0xFF == ord('q')):
        break

print fps
cap.release()
cv2.destroyAllWindows()

推荐答案

您的视频每秒可能有25帧,并且您正在逐一阅读它们.

Your video probably has 25 frames per second and you are reading them one by one.

如果这是您所期望的,则视频不在后台播放. 您的video()函数只是逐帧地对其进行迭代.

The video is not playing in the background if that is what you expected. Your video() function just iterates through it frame by frame.

解决方案是真正从设备中抢夺(read函数还可以获取设备ID,请参阅doc).

Solutions would be to really grab from the device (the read function can also take a device id, see doc).

device – id of the opened video capturing device (i.e. a camera index).

类似

# something like
cap = cv2.VideoCapture('/dev/video0') 

另一种方法是使用

VideoCapture::grab

此处

这样,您可以快速绕过想要的许多帧.

This way you can quickly bypass however many frames you want.

grab()似乎相当快,因为​​它不解码帧.因此,您可以找出您的视频每秒几帧.然后呼叫.read(),此后呼叫.grab(),只要您的视频在5秒钟内有帧即可.然后再次呼叫.read().

grab() seems to rather fast, as it does not decode the frame. So you could find out how many frames per second your video has. Then call .read() and after that call .grab() as often as your video has frames in 5 seconds. Then call .read() again.

或者真正将图片保存到磁盘上并对其进行处理.

Or really save the pictures to disk and work on them.

这篇关于如何使用opencv python和Raspbery Pi 3在摄像头每5秒钟后捕获一张图片?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆