1.采用opencv深度学习模块调用mobilenet_ssd模型进行目标检测,模型可以直接移植到移动端使用,毕竟模型不是很重,直接放线下测试代码:以供参考
import numpy as np
import cv2
deploy_prototxt = 'MobileNet-SSD/deploy.prototxt'
model = 'MobileNet-SSD/mobilenet_iter_73000.caffemodel'
confidence_default = 0.2
CLASSES = ["background", "aeroplane", "bicycle", "bird", "boat",
"bottle", "bus", "car", "cat", "chair", "cow", "diningtable",
"dog", "horse", "motorbike", "person", "pottedplant", "sheep",
"sofa", "train", "tvmonitor"]
COLORS = np.random.uniform(0, 255, size=(len(CLASSES), 3))
print("[info] loading model...")
net = cv2.dnn.readNetFromCaffe(deploy_prototxt, model)
# 打开摄像头
camera = cv2.VideoCapture('video/2c2b6d119306711be3bd7e52f7f27ca7.mp4')
print(camera)
# cv2.namedWindow('Dynamic')
width = int(camera.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(camera.get(cv2.CAP_PROP_FRAME_HEIGHT))
print(height)
fps = camera.get(cv2.CAP_PROP_FPS)
fourcc = cv2.VideoWriter_fourcc(*"mp4v")
writer = cv2.VideoWriter("video/1.mp4", fourcc, fps, (width, height))
while (True):
# 读取一帧图像
ret, frame = camera.read()
# 判断图片读取成功?
if ret:
# gray_img = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# 物体检测
blob = cv2.dnn.blobFromImage(frame, 0.007843, (width, height), 127.5)
print("[info] computing object detections...")
net.setInput(blob)
detections = net.forward()
for i in np.arange(0, detections.shape[2]):
confidence = detections[0, 0, i, 2]
if confidence > confidence_default:
idx = int(detections[0, 0, i, 1])
box = detections[0, 0, i, 3:7] * np.array([width, height, width, height])
(startX, startY, endX, endY) = box.astype("int")
label = "{}: {:.2f}%".format(CLASSES[idx], confidence * 100)
print("[info] {}".format(label))
cv2.rectangle(frame, (startX - 15, startY), (endX + 15, endY), COLORS[idx], 2)
y = startY - 15 if startY - 15 > 15 else startY + 15
cv2.putText(frame, label, (startX, y), cv2.FONT_HERSHEY_SIMPLEX, 0.5, COLORS[idx], 2)
writer.write(frame)
# 如果按下q键则退出
if cv2.waitKey(100) & 0xff == ord('q'):
break
camera.release()
writer.release()
cv2.destroyAllWindows()
代码中使用到的MobileNet_SSD可以在这里下载:提取码:2zsx
2.采用opencv做人脸检测不过效果奇差
import cv2
def DynamicDetect():
'''
打开摄像头,读取帧,检测帧中的人脸,扫描检测到的人脸中的眼睛,对人脸绘制蓝色的矩形框,对人眼绘制绿色的矩形框
'''
# 创建一个级联分类器 加载一个 .xml 分类器文件. 它既可以是Haar特征也可以是LBP特征的分类器.
face_cascade = cv2.CascadeClassifier('./haarcascade_frontalface_default.xml')
face_cascade.load('/home/work/test/opencv/data/haarcascades/haarcascade_frontalface_default.xml')
# 打开摄像头
camera = cv2.VideoCapture('video/2c2b6d119306711be3bd7e52f7f27ca7.mp4')
print(camera)
# cv2.namedWindow('Dynamic')
width = int(camera.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(camera.get(cv2.CAP_PROP_FRAME_HEIGHT))
print(height)
fps = camera.get(cv2.CAP_PROP_FPS)
fourcc = cv2.VideoWriter_fourcc(*"mp4v")
writer = cv2.VideoWriter("video/2.mp4", fourcc, fps, (width, height))
while (True):
# 读取一帧图像
ret, frame = camera.read()
# 判断图片读取成功?
if ret:
gray_img = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# 人脸检测
faces = face_cascade.detectMultiScale(gray_img, 1.3, 5)
for (x, y, w, h) in faces:
# 在原图像上绘制矩形
label = "face"
print("[info] {}".format(label))
y = x - 15 if y - 15 > 15 else y + 15
cv2.putText(frame, label, (x, y), cv2.FONT_HERSHEY_SIMPLEX, 0.5, 2)
cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2)
writer.write(frame)
# cv2.imshow('Dynamic', frame)
# 如果按下q键则退出
if cv2.waitKey(100) & 0xff == ord('q'):
break
camera.release()
writer.release()
cv2.destroyAllWindows()
if __name__ == '__main__':
DynamicDetect()
代码中用到的xml文件可以在opencv安装目录中找到,一定要用绝对路径,不然会出错。。