TODO: show img
This commit is contained in:
parent
482a07b15b
commit
478c225c6e
Binary file not shown.
|
|
@ -5,8 +5,6 @@
|
||||||
import numpy as np
|
import numpy as np
|
||||||
import tensorflow as tf
|
import tensorflow as tf
|
||||||
import cv2
|
import cv2
|
||||||
import time
|
|
||||||
import requests
|
|
||||||
|
|
||||||
|
|
||||||
class DetectorAPI:
|
class DetectorAPI:
|
||||||
|
|
@ -38,21 +36,20 @@ class DetectorAPI:
|
||||||
# Expand dimensions since the trained_model expects images to have shape: [1, None, None, 3]
|
# Expand dimensions since the trained_model expects images to have shape: [1, None, None, 3]
|
||||||
image_np_expanded = np.expand_dims(image, axis=0)
|
image_np_expanded = np.expand_dims(image, axis=0)
|
||||||
# Actual detection.
|
# Actual detection.
|
||||||
start_time = time.time()
|
|
||||||
(boxes, scores, classes, num) = self.sess.run(
|
(boxes, scores, classes, num) = self.sess.run(
|
||||||
[self.detection_boxes, self.detection_scores, self.detection_classes, self.num_detections],
|
[self.detection_boxes, self.detection_scores, self.detection_classes, self.num_detections],
|
||||||
feed_dict={self.image_tensor: image_np_expanded})
|
feed_dict={self.image_tensor: image_np_expanded})
|
||||||
end_time = time.time()
|
|
||||||
|
|
||||||
print("Elapsed Time:", end_time-start_time)
|
|
||||||
|
|
||||||
im_height, im_width,_ = image.shape
|
im_height, im_width,_ = image.shape
|
||||||
boxes_list = [None for i in range(boxes.shape[1])]
|
boxes_list = [None for i in range(boxes.shape[1])]
|
||||||
for i in range(boxes.shape[1]):
|
for i in range(boxes.shape[1]):
|
||||||
boxes_list[i] = (int(boxes[0,i,0] * im_height),
|
boxes_list[i] = (
|
||||||
int(boxes[0,i,1]*im_width),
|
int(boxes[0,i,0] * im_height),
|
||||||
int(boxes[0,i,2] * im_height),
|
int(boxes[0,i,1] * im_width),
|
||||||
int(boxes[0,i,3]*im_width))
|
int(boxes[0,i,2] * im_height),
|
||||||
|
int(boxes[0,i,3] * im_width)
|
||||||
|
)
|
||||||
|
|
||||||
return boxes_list, scores[0].tolist(), [int(x) for x in classes[0].tolist()], int(num[0])
|
return boxes_list, scores[0].tolist(), [int(x) for x in classes[0].tolist()], int(num[0])
|
||||||
|
|
||||||
|
|
@ -60,32 +57,38 @@ class DetectorAPI:
|
||||||
self.sess.close()
|
self.sess.close()
|
||||||
self.default_graph.close()
|
self.default_graph.close()
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
model_path = "C:/Users/John/Desktop/ster_rcnn_inception_v2_coco_2018_01_28/ster_rcnn_inception_v2_coco_2018_01_28/ozen_inference_graph.pb"
|
class Detector:
|
||||||
odapi = DetectorAPI(path_to_ckpt=model_path)
|
def __init__(self, stream):
|
||||||
threshold = 0.3
|
self.model_path = "./model.pb"
|
||||||
|
self.odapi = DetectorAPI(path_to_ckpt=self.model_path)
|
||||||
|
self.threshold = 0.3
|
||||||
|
self.stream = stream
|
||||||
|
|
||||||
|
|
||||||
while True:
|
def detect(self):
|
||||||
cap = cv2.VideoCapture("http://69.254.67.229:8081/mjpg/video.mjpg")
|
cap = cv2.VideoCapture(self.stream)
|
||||||
r, img = cap.read()
|
r, img = cap.read()
|
||||||
img = cv2.resize(img, (500, 500))
|
img = cv2.resize(img, (500, 500))
|
||||||
|
|
||||||
boxes, scores, classes, num = odapi.processFrame(img)
|
boxes, scores, classes, num = self.odapi.processFrame(img)
|
||||||
|
|
||||||
# Visualization of the results of a detection.
|
# Visualization of the results of a detection.
|
||||||
|
|
||||||
for i in range(len(boxes)):
|
for i in range(len(boxes)):
|
||||||
# Class 1 represents human
|
# Class 1 represents human
|
||||||
if classes[i] == 1:
|
if classes[i] == 1:
|
||||||
if scores[i] > threshold:
|
if scores[i] > self.threshold:
|
||||||
box = boxes[i]
|
box = boxes[i]
|
||||||
cv2.rectangle(img,(box[1],box[0]),(box[3],box[2]),(255,0,0),2)
|
cv2.rectangle(img,(box[1],box[0]),(box[3],box[2]),(255,0,0),2)
|
||||||
requests.get("http://192.168.178.53/play")
|
print("yes")
|
||||||
|
return True, img
|
||||||
else:
|
else:
|
||||||
requests.get("http://192.168.178.53/stop")
|
print("no")
|
||||||
|
|
||||||
cv2.imshow("preview", img)
|
return False, img
|
||||||
key = cv2.waitKey(1)
|
cv2.imshow("preview", img) # cv2.destroyWindow("preview")
|
||||||
if key & 0xFF == ord('q'):
|
|
||||||
break
|
def __del__(self):
|
||||||
|
self.cap.release()
|
||||||
|
cv2.destroyAllWindows()
|
||||||
|
|
@ -0,0 +1,31 @@
|
||||||
|
import time
|
||||||
|
import requests
|
||||||
|
import detector as dt
|
||||||
|
import cv2
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
t = 30 # seconds a person can leave the room for
|
||||||
|
t0 = time.time()
|
||||||
|
time.clock()
|
||||||
|
elapsed = 0
|
||||||
|
stream = "http://217.128.254.187:8083/mjpg/video.mjpg"
|
||||||
|
|
||||||
|
detector = dt.Detector(stream)
|
||||||
|
music_playing = False
|
||||||
|
|
||||||
|
while True:
|
||||||
|
elapsed = time.time() - t0
|
||||||
|
if elapsed > t and music_playing:
|
||||||
|
r = requests.get("http://192.168.178.53/stop")
|
||||||
|
if r.status_code == 200:
|
||||||
|
music_playing = False
|
||||||
|
|
||||||
|
result, img = detector.detect()
|
||||||
|
if result and not music_playing:
|
||||||
|
r = requests.get("http://192.168.178.53/play")
|
||||||
|
if r.status_code == 200:
|
||||||
|
music_playing = True
|
||||||
|
t0 = time.time()
|
||||||
|
cv2.imshow("preview", img) # cv2.destroyWindow("preview")
|
||||||
|
time.sleep(1)
|
||||||
|
|
||||||
Loading…
Reference in New Issue