import sys,os
import numpy as np
import time
import threading
import copy
import Queue
import signal
import logging
sys.path.append("/usr/local/lib/python2.7/site-packages")
import cv2
caffe_root = '/opt/cpf/cpf-ai/clcaffe-mkl/'
sys.path.insert(0, caffe_root + 'python')
import caffe
sys.path.append('/opt/cpf/cpf-ai/board-master')
import ai_logger
from Board import Board
net_file= '/opt/cpf/cpf-ai/clcaffe-mkl/model/objectDetect/fused.MobileNetSSD_deploy.no_engine_upgraded.prototxt'
caffe_model='/opt/cpf/cpf-ai/clcaffe-mkl/model/objectDetect/fused.MobileNetSSD_deploy.caffemodel'
log = logging.getLogger('cpf-ai')
log.setLevel('INFO')
log.addHandler(ai_logger.AIloggerHandler())
caffe.set_mode_gpu()
caffe.set_device(0)
CLASSES = ('background',
'aeroplane', 'bicycle', 'bird', 'boat',
'bottle', 'bus', 'car', 'cat', 'chair',
'cow', 'diningtable', 'dog', 'horse',
'motorbike', 'person', 'pottedplant',
'sheep', 'sofa', 'train', 'tvmonitor')
if not os.path.exists(net_file):
log.error("prototxt file doesn't exit")
exit()
if not os.path.exists(caffe_model):
log.error("caffemodel file doesn't exit")
exit()
class ipcamCapture:
def __init__(self):
self.Frame = []
self.RequestBody = None
self.status = False
self.isstop = False
self.detectsQueue = Queue.Queue(maxsize = 1)
self.camFrameQueue = Queue.Queue(maxsize = 1)
self.frame_box = 0
self.frame_conf = 0
self.frame_class = 0
self.noDetectCount = 0
self.noDetectThreshold = 10
self.capture = cv2.VideoCapture(0)
# init caffe and mobilenet ssd
self.net = caffe.Net(net_file,caffe_model,caffe.TEST)
def preprocess(self, src):
img = cv2.resize(src, (300,300))
img = img - 127.5
img = img * 0.007843
return img
def postprocess(self, img, out):
h = img.shape[0]
w = img.shape[1]
box = out['detection_out'][0,0,:,3:7] * np.array([w, h, w, h])
cls = out['detection_out'][0,0,:,1]
conf = out['detection_out'][0,0,:,2]
return (box.astype(np.int32), conf, cls)
def detect(self, net, frame):
log.debug('detect started!')
framecopy = copy.copy(frame)
img = self.preprocess(framecopy)
img = img.astype(np.float32)
img = img.transpose((2, 0, 1))
net.blobs['data'].data[...] = img
log.debug("net.forward() - start")
self.RequestBody = '["digitalWrite", {:0} , {:0}]'.format(6,1)
start = time.clock()
out = net.forward()
end = time.clock()
self.RequestBody = '["digitalWrite", {:0} , {:0}],["grove_setColorRGB", {:0}, {:0}, {:0}, {:0}]'.format(6,0,0,0,0,0)
log.debug("net.forward() - end")
log.info('detection time: %f s' % (end - start))
box, conf, cls = self.postprocess(frame, out)
return box, conf, cls
def drawFrameRects (self, img, box, conf, cls):
for i in range(len(box)):
title = "%s:%.2f" % (CLASSES[int(cls[i])], conf[i])
log.debug("res: " + title)
if CLASSES[int(cls[i])] == "person" and conf[i] > 0.8:
log.info("res: " + title)
self.RequestBody = '["grove_setColorRGB", {:0}, {:0}, {:0}, {:0}]'.format(0,255,0,0)
def start(self):
log.info('ipcam started!')
t3 = threading.Thread(target=self.detectBoard, args=())
t3.setDaemon(True)
t3.start()
t = threading.Thread(target=self.queryframe, args=())
t.setDaemon(True)
t.start()
def stop(self):
self.isstop = True
log.info('ipcam stopped!')
def getframe(self):
return self.Frame
def queryframe(self):
log.info('started capture!')
while (not self.isstop):
self.status, frame = self.capture.read()
if self.status == True:
self.Frame = cv2.cvtColor(frame,cv2.COLOR_BGR2RGB)
if self.camFrameQueue.empty():
self.camFrameQueue.put(self.Frame)
if not self.detectsQueue.empty():
box,conf,cls = self.detectsQueue.get()
if box is not None:
self.frame_box = box
self.frame_conf = conf
self.frame_class = cls
self.noDetectCount = 0
else:
self.noDetectCount = self.noDetectCount + 1
if self.noDetectCount >= self.noDetectThreshold:
self.frame_box = 0
self.frame_conf = 0
self.frame_class = 0
if self.frame_box is not 0:
self.drawFrameRects (self.Frame, self.frame_box, self.frame_conf, self.frame_class)
else:
log.warning("no webcam data")
time.sleep(3)
self.capture.release()
log.info('queryframe stop!')
def detectframe(self):
log.debug('detectframe started!')
while (not self.isstop):
if not self.camFrameQueue.empty() :
frame = self.camFrameQueue.get()
detects = self.detect(self.net, frame)
self.detectsQueue.put(detects)
log.debug('detectframe stop!')
def detectBoard(self):
isInit = False
while (not self.isstop):
for port in Board().getPort():
log.debug(port)
if Board().boardReady(port):
if (not isInit):
body = '["resetPin"],["setPinMode", "digital", {:0}, "OUTPUT"],["grove_newChainableLED",{:0}, {:0}, {:0}]'.format(6,7,8,1)
Board().setPinMode(port, body)
body = '["digitalWrite", {:0} , {:0}],["grove_setColorRGB", {:0}, {:0}, {:0}, {:0}]'.format(6,0,0,0,0,0)
Board().request(port, body)
isInit = True
else:
body = self.RequestBody
if body is not None:
log.debug(body)
Board().request(port, body)
else:
isInit = False
time.sleep(1)
# Usage: python example_webCamAIoT_objectDetection.py
# Press ctrl+c exit the program
try:
ipcam = ipcamCapture()
ipcam.start()
time.sleep(3)
ipcam.detectframe()
except KeyboardInterrupt:
log.info('KeyboardInterrupt')
sys.exit(0)