但我们需要对此处进行修改,使用rtsp流,进行AI行人识别,下面需要进行分析代码,找到可以修改的地方,或者摸个参数,来进行RTSP流的修改。
已经找到了视频流在哪里传进去的了,下面就是进行分析里面的代码进行改成rtsp流,把rtsp流写进去,来做到实时分析,实现行人检测的效果。
在进行分析的时候,发现source这个参数只有LoadStreams用到过,而且是直接传进去的。
进入source参数里面查看,发现里面有一个默认的值,就是读文件,如果不是文件,就把source=[source],再进行source值遍历。在遍历中还使用到了opencv打开本地电脑的摄像机流,再开一个线程进行实时行人识别。
代码中使用了opencv中cv2.VideoCapture的函数,从网上查找这个函数的用法得知,此函数是可以直接传入rtsp流地址的,所以问题解决就简单多了。cv2.VideoCapture这个函数是可以传入rtsp地址的,所以传入rtsp地址进行尝试,发现传入rtsp地址是没有问题的。
只要修改source这个参数即可,最终实现了检测:
可以使用OpenCV,OpenCV的人脸检测功能在一般场合还是不错的。而ubuntu正好提供了python-opencv这个包,用它可以方便地实现人脸检测的代码。
写代码之前应该先安装python-opencv:
#!/usr/bin/python# -*- coding: UTF-8 -*-
# face_detect.py
# Face Detection using OpenCV. Based on sample code from:
# http://python.pastebin.com/m76db1d6b
# Usage: python face_detect.py <image_file>
import sys, os
from opencv.cv import *
from opencv.highgui import *
from PIL import Image, ImageDraw
from math import sqrt
def detectObjects(image):
"""Converts an image to grayscale and prints the locations of any faces found"""
grayscale = cvCreateImage(cvSize(image.width, image.height), 8, 1)
cvCvtColor(image, grayscale, CV_BGR2GRAY)
storage = cvCreateMemStorage(0)
cvClearMemStorage(storage)
cvEqualizeHist(grayscale, grayscale)
cascade = cvLoadHaarClassifierCascade(
'/usr/share/opencv/haarcascades/haarcascade_frontalface_default.xml',
cvSize(1,1))
faces = cvHaarDetectObjects(grayscale, cascade, storage, 1.1, 2,
CV_HAAR_DO_CANNY_PRUNING, cvSize(20,20))
result = []
for f in faces:
result.append((f.x, f.y, f.x+f.width, f.y+f.height))
return result
def grayscale(r, g, b):
return int(r * .3 + g * .59 + b * .11)
def process(infile, outfile):
image = cvLoadImage(infile)
if image:
faces = detectObjects(image)
im = Image.open(infile)
if faces:
draw = ImageDraw.Draw(im)
for f in faces:
draw.rectangle(f, outline=(255, 0, 255))
im.save(outfile, "JPEG", quality=100)
else:
print "Error: cannot detect faces on %s" % infile
if __name__ == "__main__":
process('input.jpg', 'output.jpg')