opencv環境
1、訪問Python Extension Packages for Windows,下載python對應版本的opencv。
比如小編下載的是opencv_python-3.3.0+contrib-cp36-cp36m-win_amd64.whl,cp36表示Python是3.6版本,win_amd64是表示安裝的python是64bit的,+contrib表示包括contrib包。
2、下載好后,把它放到C盤中,執行安裝命令:
pip install C:\opencv_python-3.3.0+contrib-cp36-cp36m-win_amd64.whl
運行代碼
修改
從本地獲取。
# vs = VideoStream(src=0).start() # vs =cv2.VideoCapture('C:\\Users\\voidking\\Desktop\\real-time-object-detection\\test_video.flv')
vs =cv2.VideoCapture('./test_video.flv')
# grab the frame from the threaded video stream and resize it # to have a maximum width of 400 pixels # frame = vs.read() # frame = imutils.resize(frame, width=400)
# grab the frame from the threaded video file stream
(grabbed,frame) = vs.read() # if the frame was not grabbed, then we have reached the end # of the stream
if not grabbed: break frame = imutils.resize(frame, width=800)
運行
推薦使用命令:
python real_time_object_detection.py -p ./MobileNetSSD_deploy.prototxt.txt -m ./MobileNetSSD_deploy.caffemodel
或者,指定絕對路徑,假設項目目錄為C:\Users\voidking\Desktop\real-time-object-detection\
,那么命令如下:
python real_time_object_detection.py -p "C:\Users\voidking\Desktop\real-time-object-detection\MobileNetSSD_deploy.prototxt.txt" -m "C:\Users\voidking\Desktop\real-time-object-detection\MobileNetSSD_deploy.caffemodel"
進階修改
我們看到,prototxt和model都是指定的,那我們的視頻文件也用這種方式指定,就更加友好一點。
# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser() ap.add_argument("-p", "--prototxt", required=True, help="path to Caffe 'deploy' prototxt file") ap.add_argument("-m", "--model", required=True, help="path to Caffe pre-trained model") ap.add_argument("-c", "--confidence", type=float, default=0.2, help="minimum probability to filter weak detections") args = vars(ap.parse_args())
我們插入一行:
ap.add_argument("-v", "--video", required=True, help="path to Caffe video file")
然后在初始化視頻流時,修改為:
vs =cv2.VideoCapture(args["video"])
運行命令修改為
python real_time_object_detection.py -p ./MobileNetSSD_deploy.prototxt.txt -m ./MobileNetSSD_deploy.caffemodel -v ./test_video.flv
運行效果
源碼分享
https://gitee.com/lyc96/real-time_video_target_detection
關注公眾號:Python爬蟲數據分析挖掘,學習更多python知識