监控器物检测object detection实战

实战目的:根据家里的监控器,实时检测出监控拍到的物体,包括人、车等。


基本情况:

1、家里安装有JOVISION监控器(中维),摄像头与存储设备通过路由器连接。

2、JOVISION对应有一个客户端,名称为:云视通网络监控系统,可以在JOVISION官网下载。

——————————————————————————————————————————

思路:

用一个训练好的小的Faster RCNN模型,结合opencv,将摄像头采集到图片输送模型,

标注出图片中检测出的物体。

——————————————————————————————————————————

困难:

JOVISION客户端处理监控视频有两种方法:(1)点击,录制好一段存起来;(2)点一个按钮保存一个快照

这两种方法都不好实施,方案(1)也需要点击,并且视频存储起来后才可使用,无法做到实时,方案(2)

如果能手动一直操作鼠标点击保存快照,而另一边程序实时检测快照图片,那就能做到实时。

但人工一直点击不现实。

——————————————————————————————————————————

解决方案:

用按键精灵编写了一个小脚本,用于定时点击监控,定时点击快照生成按钮。

按键精灵代码:

Plugin.Window.MousePoint 
点击 = WaitClick()
Delay 10
Hwnd = Plugin.Window.MousePoint()
GetCursorPos x, y
sRect = Plugin.Window.GetClientRect(Hwnd)
MyArray = Split(sRect, "|")
L = CLng(MyArray(0))
T = CLng(MyArray(1))
R = CLng(MyArray(2))
B = CLng(MyArray(3))

点击1 = WaitClick()
Delay 10
Hwnd1 = Plugin.Window.MousePoint()
GetCursorPos x1, y1
sRect1 = Plugin.Window.GetClientRect(Hwnd1)
MyArray1 = Split(sRect1, "|")
L1 = CLng(MyArray1(0))
T1 = CLng(MyArray1(1))
R1 = CLng(MyArray1(2))
B1 = CLng(MyArray1(3))

Tick = 2000
Do
Delay Tick
Call Plugin.Bkgnd.LeftClick(Hwnd1, x1 - L1, y1 - T1)
Delay Tick
Call Plugin.Bkgnd.LeftClick(Hwnd, x - L, y - T)
Loop

按键精灵按F5运行,切换到JOVISION客户端,按F10启动,然后操作。

按键精灵界面:

监控器物检测object detection实战_第1张图片

JOVISION客户端界面:

监控器物检测object detection实战_第2张图片

监控快照需要首先点击需要截图的监控页面,然后点击按钮“现场拍照”

——————————————————————————————————————————

生成的快照图片被保存在一个文件夹中。

物体检测代码:

# USAGE
# python deep_learning_object_detection_files.py --prototxt MobileNetSSD_deploy.prototxt.txt --model MobileNetSSD_deploy.caffemodel
# import the necessary packages
import numpy as np
import argparse
import time
import cv2
import os


FindPath = "E:\\JdvrFile\\CapFile\\"
def deleteimage(fullfilename):
os.remove(fullfilename)
# 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())


# initialize the list of class labels MobileNet SSD was trained to
# detect, then generate a set of bounding box colors for each class
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))


# load our serialized model from disk
print("[INFO] loading model...")
net = cv2.dnn.readNetFromCaffe(args["prototxt"], args["model"])


while True:
FileNames = os.listdir(FindPath)
for file_name in FileNames:
fullfilename = os.path.join(FindPath,file_name)
# print "fullfilename:", fullfilename
image = cv2.imread(fullfilename)
if image is None:
continue
(h, w) = image.shape[:2]
blob = cv2.dnn.blobFromImage(cv2.resize(image, (300, 300)), 0.007843, (300, 300), 127.5)
net.setInput(blob)
detections = net.forward()
for i in np.arange(0,detections.shape[2]):
confidence = detections[0,0,i,2]
if confidence > args["confidence"]:
idx = int(detections[0,0,i,1])
box = detections[0,0,i,3:7] * np.array([w,h,w,h])
(startX,startY,endX,endY) = box.astype("int")
label = "{}: {:.2f}%".format(CLASSES[idx],confidence*100)
cv2.rectangle(image,(startX,startY),(endX,endY),COLORS[idx],2)
y = startY - 15 if startY - 15 > 15 else startY + 15
cv2.putText(image,label,(startX,y),
cv2.FONT_HERSHEY_SIMPLEX,0.5,COLORS[idx],2)
cv2.imshow("Frame",image)
key = cv2.waitKey(1)
deleteimage(fullfilename)

——————————————————————————————————————————

物体检测代码可以参考github:https://github.com/miracleyhj/object-detection-with-jovision-monitor

关于模型,可以参考博客:https://www.pyimagesearch.com/2017/09/11/object-detection-with-deep-learning-and-opencv/

——————————————————————————————————————————

欢迎微信骚扰:yuan136315miracle

你可能感兴趣的:(监控器物检测object detection实战)