class VideoFileCamera(Thread): def __init__(self, video_file, queue): super().__init__() self.queue = queue self.video_file = video_file self.THRESHOLD = 0.25 self.detecter = Detecter() def run(self): cap = cv2.VideoCapture(self.video_file) fps = cap.get(cv2.CAP_PROP_FPS) delay = int(1000/fps) # detecter = Detecter() self.detecter.setup('./frozen_inference_graph.pb', './mscoco_label_map.pbtxt') while(cap.isOpened()): _, frame = cap.read() frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) # frame_ex = np.expand_dims(frame, axis=0) # (boxes, scores, classes, num) = self.detecter.detect(frame_ex) # self.detecter.viaulize(frame, boxes, classes, scores, self.THRESHOLD) self.queue.put(frame, timeout=2) cv2.waitKey(delay) cap.release() cv2.destroyAllWindows()
class Objectdetecter: def __init__(self): self.detecter = Detecter() self.detecter.setup( './data/ssd_mobilenet_v1/frozen_inference_graph.pb', './data/label_map.pbtxt') self.THRESHOLD = 0.8 def __check(self, image): detect_image = np.copy(image) image = np.expand_dims(image, axis=0) (boxes, scores, classes, num) = self.detecter.detect(image) list = [] self.detecter.viaulize(detect_image, boxes, classes, scores, self.THRESHOLD) for output in zip(classes, scores, boxes): if output[1] >= self.THRESHOLD: y1, x1, y2, x2 = output[2] y = int((y2 - y1) * 480) list.append((output, y)) return list, detect_image def order(self, image): lists, detect_image = self.__check(image) sign = 5 tra = 5 st = 300 fw = 5 for list in lists: if list[0][0] == 5: c = list[1] if c > sign: return 'stopsign', detect_image elif list[0][0] == 6: c = list[1] if c > tra: return 'trash', detect_image elif list[0][0] == 1: c = list[1] if c < st: return 'stop', detect_image elif list[0][0] == 2: c = list[1] if c > fw: return 'forward', detect_image return None, detect_image
class ImageViewer(Thread): def __init__(self, name, queue): super().__init__() self.name = name self.queue = queue self.THRESHOLD = 0.25 self.detecter = Detecter() self.detecter.setup('./frozen_inference_graph.pb', './mscoco_label_map.pbtxt') pass def run(self): while (True): frame = self.queue.get(timeout=2) frame_ex = np.expand_dims(frame, axis=0) (boxes, scores, classes, num) = self.detecter.detect(frame_ex) self.detecter.viaulize(frame, boxes, classes, scores, self.THRESHOLD) cv2.imshow(self.name, frame) if cv2.waitKey(1) & 0xFF == ord('q'): break cv2.destroyAllWindows() pass
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) image_ex = np.expand_dims(frame, axis=0) (boxes, scores, classes, num) = detecter.detect(image_ex) detecter.viaulize(frame, boxes, classes, scores, THRESHOLD) cv2.imshow('people', frame) def play1(): print(path) cap = cv2.VideoCapture(path) while(cap.isOpened()): ret, frame = cap.read() detect1(frame) if cv2.waitKey(1) & 0xFF == ord('q'): break cap.release() cv2.destroyAllWindows() if __name__ == "__main__": detecter = Detecter() detecter.setup('./frozen_inference_graph.pb','./mscoco_label_map.pbtxt') THRESHOLD = 0.3 path = "vtest.avi" t = Thread(target=play1, args=()) t.start()