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()
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
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
def job(): stack=[] PATH="data/images/" date=datetime.datetime.now().strftime("%Y-%m-%d") target =date+".mp4" os.mkdir(PATH+date) cam = cv2.VideoCapture("data/videos/" + target) minute=datetime.datetime.now().minute FPS = 1 try: if not os.path.exists('data'): os.makedirs('data') except OSError: print('Error: Creating directory of data') currentframe = 0 capture_rate = 60#int(cam.get(cv2.CAP_PROP_FPS)) end_of_frame = cam.get(cv2.CAP_PROP_FRAME_COUNT) # print(end_of_frame) stat_of_frame =0# minute*(cam.get(cv2.CAP_PROP_FPS) * 60) # print(stat_of_frame) for i in range(TOTAL_MIN//SAMPLING_CYCLE): for j in range(SAMPLING_CYCLE): cam.set(cv2.CAP_PROP_POS_FRAMES, CAPTURE_CYCLE * (currentframe*FPS) + stat_of_frame) ret, frame = cam.read() if ret: # if currentframe % capture_rate == 0: name = 'data/frames/frame' + str(j) + '.jpg' cv2.imwrite(name, frame) if j == 0: # print(PATH+date+'/'+str(i)) cv2.imwrite(PATH+date+'/'+str(i)+'.jpg', frame) else: break currentframe += 1 result = Detecter.detect() output=round(result/CAPTURE_CYCLE,2) # print(datetime.datetime.now().strftime("%Y-%m-%d"),i,": Avg =",output) stack.append(output) plt.plot(range(TOTAL_MIN//SAMPLING_CYCLE),stack) plt.savefig(target+'result.png') mosaicer.process(PATH+date+'/') cam.release() cv2.destroyAllWindows()
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()
import cv2 import numpy as np from picamera.array import PiRGBArray from picamera import PiCamera from detecter import Detecter THRESHOLD = 0.3 detecter = Detecter() detecter.setup('./frozen_inference_graph.pb', './mscoco_label_map.pbtxt') # 파이카메라 준비 # 카메라 해상도 IM_WIDTH = 640 IM_HEIGHT = 480 # PiCamera 설정 camera = PiCamera() camera.resolution = (IM_WIDTH, IM_HEIGHT) camera.framerate = 10 rawCapture = PiRGBArray(camera, size=(IM_WIDTH, IM_HEIGHT)) rawCapture.truncate(0) for frame1 in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True): image = np.copy(frame1.array) image.setflags(write=1) image_ex = np.expand_dims(image, axis=0) # detection (boxes, scores, classes, num) = detecter.detect(image_ex)
def __init__(self, video_file, queue): super().__init__() self.queue = queue self.video_file = video_file self.THRESHOLD = 0.25 self.detecter = Detecter()
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
import cv2 import os from detecter import Detecter from mosaicer import mosaicer import schedule import time import datetime import matplotlib.pyplot as plt from config import SAMPLING_CYCLE,TOTAL_MIN,CAPTURE_CYCLE Detecter = Detecter() mosaicer = mosaicer() def job(): stack=[] PATH="data/images/" date=datetime.datetime.now().strftime("%Y-%m-%d") target =date+".mp4" os.mkdir(PATH+date) cam = cv2.VideoCapture("data/videos/" + target) minute=datetime.datetime.now().minute FPS = 1 try: if not os.path.exists('data'): os.makedirs('data') except OSError: print('Error: Creating directory of data')