def main(self): # cap = cv2.VideoCapture(1) # cap = cv2.VideoCapture('/dev/video2') cap = cv2.VideoCapture('real_yolo.avi') cap.set( 3, WEIGHT ) # Feed resolution from webcam to YOLO however output is from yolo cfg file resolution cap.set(4, HEIGHT) out = cv2.VideoWriter( "output.avi", cv2.VideoWriter_fourcc(*"MJPG"), 20.0, (darknet.network_width(netMain), darknet.network_height(netMain))) print("Starting the YOLO loop...") # Create an image we reuse for each detect darknet_image = darknet.make_image(darknet.network_width(netMain), darknet.network_height(netMain), 3) # Variables frame_count = countimg = 0 turn = mx = frame_show = '' self.last_mission = '' self.TF_flag = [0, 0, 0] while True: self.mission = '' self.school = 0 prev_time = time.time() ret, frame_read = cap.read() frame_rgb = cv2.cvtColor(frame_read, cv2.COLOR_BGR2RGB) frame_resized = cv2.resize( frame_rgb, (darknet.network_width(netMain), darknet.network_height(netMain)), interpolation=cv2.INTER_LINEAR) # CHECK RESIZING KEEPING RATIO darknet.copy_image_from_bytes(darknet_image, frame_resized.tobytes()) detections = darknet.detect_image(netMain, metaMain, darknet_image, THRESH) detect = self.checkSize(detections) fps = (1 / (time.time() - prev_time)) image, x1, x2, y1, y2 = self.cvDrawBoxes(detections, frame_resized) image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) if detect: sg1 = detect[0][0].decode() prec1 = detect[0][1] # If there are 2 readings in a frame, parse both at the same time (fix for single parsing per frame. Only 2 because no need for 3+) if len(detect) == 2: sg2 = detect[1][0].decode() prec2 = detect[1][1] if sg2 == 'red_light' or sg2 == 'green_light': self.TF_flag = lightsParser(image, x1, x2, y1, y2) if self.TF_flag is None: pass elif self.TF_flag[0] == 1: self.sign['red_light'][0] += 1 elif self.TF_flag[1] == 1: self.sign['arrow_light'][0] += 1 elif self.TF_flag[2] == 1: self.sign['green_light'][0] += 1 else: self.TF_flag = [0, 0, 0] self.sign[str(sg2)][0] += 1 self.sign[str(sg2)][1] += prec2 if sg1 == 'red_light' or sg1 == 'green_light': self.TF_flag = lightsParser(image, x1, x2, y1, y2) if self.TF_flag is None: pass elif self.TF_flag[0] == 1: self.sign['red_light'][0] += 1 elif self.TF_flag[1] == 1: self.sign['arrow_light'][0] += 1 print('arrow') elif self.TF_flag[2] == 1: self.sign['green_light'][0] += 1 else: self.TF_flag = [0, 0, 0] self.sign[sg1][0] += 1 self.sign[sg1][1] += prec1 mx = max(self.sign, key=lambda key: self.sign[key][0]) if self.sign[mx][0]: frame_show = str(self.sign[mx][0]) frame_count = 0 else: frame_count += 1 # In order to know IDLE, count frames without any reading and store in frame_count self.high_vals = { k: v for (k, v) in self.sign.items() if v[0] >= SIZE } # Check if there are any values above size limit if self.high_vals: # If there is any sign > SIZE max_sign = max( self.high_vals, key=lambda key: self.high_vals[key][0] ) # Finds the key with highest frame_count on the dictionary max_precis = max( self.high_vals, key=lambda key: self.high_vals[key][1] ) # Finds the key with highest precision on the dictionary counter = self.high_vals[max_sign][ 0] # Finds the value of the highest frame_count on the dictionary precis = self.high_vals[max_precis][ 1] # Finds the value of the highest precision on the dictionary if 'left' in self.high_vals and self.sign['left'][ 0] > self.sign['right'][0] and self.sign['left'][ 0] > self.sign['intersection'][ 0]: # Left is the highest between right and front turn = 'left' self.mission = self.check_lights(turn) elif 'right' in self.high_vals and self.sign['right'][ 0] > self.sign['left'][0] and self.sign['right'][ 0] > self.sign['intersection'][ 0]: # Right is the highest between left and front self.mission = 'right' self.reset_dict(0) elif 'intersection' in self.high_vals and self.sign[ 'intersection'][0] > self.sign['right'][0] and self.sign[ 'intersection'][0] > self.sign['left'][ 0]: # Front is the highest between right and left turn = 'front' self.mission = self.check_lights(turn) elif max_sign == 'school': self.mission = max_sign self.school = 1 self.reset_dict(0) elif max_sign == 'green_light' or max_sign == 'red_light': # Situation where car only sees traf. lights -> Go to last saved turn self.mission = self.check_lights(turn) else: # All other missions that dont use traffic lights self.mission = max_sign self.reset_dict( 0 ) # 0 = reset all 1 = reset all but left right inter green self.last_mission = self.mission # Update last mission elif frame_count > IDLE_SIZE: # If No detection for the last IDLE_SIZE frames -> refresh all (used to avoid random uncertain and uncontinuos detections) self.reset_dict(0) mx = 'SEARCHING' frame_show = '' frame_count = 0 self.last_mission = 'default' self.TF_flag = [0, 0, 0] m_number = self.get_mission_number() # DEBUGING: write frame count, last mission, the sign with max count and its count, fps cv2.putText(image, str(frame_count), bottomLeftCornerOfText, font, fontScale, fontColor, lineType) cv2.putText(image, self.last_mission, (450, 600), font, fontScale, fontColor, lineType) cv2.putText(image, mx, (400, 570), font, fontScale, fontColor, lineType) cv2.putText(image, frame_show, (550, 570), font, fontScale, fontColor, lineType) cv2.putText(image, str(m_number), (10, 370), font, fontScale, fontColor, lineType) cv2.putText(image, self.mission, (10, 400), font, fontScale, fontColor, lineType) cv2.putText(image, str(self.TF_flag), (10, 570), font, fontScale, fontColor, lineType) cv2.putText(image, 'fps:' + str(int(fps)), (550, 30), font, 0.5, fontColor, lineType) cv2.rectangle(image, (0, 0), (10, 10), (60, 60, 60), 1) cv2.rectangle(image, (0, 0), (316, 316), (60, 60, 60), 1) out.write(image) # P cv2.imshow('Demo', image) cv2.waitKey(3)
def YOLO(): global metaMain, netMain, altNames configPath = "./cfg/yolov3.cfg" weightPath = "./yolov3.weights" metaPath = "./cfg/coco.data" if not os.path.exists(configPath): raise ValueError("Invalid config path `" + os.path.abspath(configPath) + "`") if not os.path.exists(weightPath): raise ValueError("Invalid weight path `" + os.path.abspath(weightPath) + "`") if not os.path.exists(metaPath): raise ValueError("Invalid data file path `" + os.path.abspath(metaPath) + "`") if netMain is None: netMain = darknet.load_net_custom(configPath.encode("ascii"), weightPath.encode("ascii"), 0, 1) # batch size = 1 if metaMain is None: metaMain = darknet.load_meta(metaPath.encode("ascii")) if altNames is None: try: with open(metaPath) as metaFH: metaContents = metaFH.read() import re match = re.search("names *= *(.*)$", metaContents, re.IGNORECASE | re.MULTILINE) if match: result = match.group(1) else: result = None try: if os.path.exists(result): with open(result) as namesFH: namesList = namesFH.read().strip().split("\n") altNames = [x.strip() for x in namesList] except TypeError: pass except Exception: pass #cap = cv2.VideoCapture(0) cap = cv2.VideoCapture("test.mp4") cap.set(3, 1280) cap.set(4, 720) out = cv2.VideoWriter( "output.avi", cv2.VideoWriter_fourcc(*"MJPG"), 10.0, (darknet.network_width(netMain), darknet.network_height(netMain))) print("Starting the YOLO loop...") # Create an image we reuse for each detect darknet_image = darknet.make_image(darknet.network_width(netMain), darknet.network_height(netMain), 3) while True: prev_time = time.time() ret, frame_read = cap.read() frame_rgb = cv2.cvtColor(frame_read, cv2.COLOR_BGR2RGB) frame_resized = cv2.resize( frame_rgb, (darknet.network_width(netMain), darknet.network_height(netMain)), interpolation=cv2.INTER_LINEAR) darknet.copy_image_from_bytes(darknet_image, frame_resized.tobytes()) detections = darknet.detect_image(netMain, metaMain, darknet_image, thresh=0.25) image = cvDrawBoxes(detections, frame_resized) image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) print(1 / (time.time() - prev_time)) cv2.imshow('Demo', image) cv2.waitKey(3) cap.release() out.release()
def YOLO(): global metaMain, netMain, altNames # weightPath = "./model/50_1024_best.weights" weightPath = "./model/yolov3-tiny-card_best.weights" configPath = "./model/yolov3-tiny-card.cfg" metaPath = "./model/obj.data" if not os.path.exists(configPath): raise ValueError("Invalid config path `" + os.path.abspath(configPath) + "`") if not os.path.exists(weightPath): raise ValueError("Invalid weight path `" + os.path.abspath(weightPath) + "`") if not os.path.exists(metaPath): raise ValueError("Invalid data file path `" + os.path.abspath(metaPath) + "`") if netMain is None: netMain = darknet.load_net_custom(configPath.encode("ascii"), weightPath.encode("ascii"), 0, 1) # batch size = 1 if metaMain is None: metaMain = darknet.load_meta(metaPath.encode("ascii")) if altNames is None: try: with open(metaPath) as metaFH: metaContents = metaFH.read() import re match = re.search("names *= *(.*)$", metaContents, re.IGNORECASE | re.MULTILINE) if match: result = match.group(1) else: result = None try: if os.path.exists(result): with open(result) as namesFH: namesList = namesFH.read().strip().split("\n") altNames = [x.strip() for x in namesList] except TypeError: pass except Exception: pass #initialization num_players = setup() clusters = [] start = True # initialize webcam input (if there is an error, switch to VideoCapture(1)) cap = cv2.VideoCapture(0) cap.set(3, 1280) cap.set(4, 720) # init for video input # cap = cv2.VideoCapture("./test/1player.mp4") # cap = cv2.VideoCapture("./test/2player.mp4") # cap.set(3, 1280) # cap.set(4, 720) # out = cv2.VideoWriter('2player_res.avi',cv2.VideoWriter_fourcc('M','J','P','G'), 10.0, (darknet.network_width(netMain), darknet.network_height(netMain))) print("Starting the YOLO loop...") # Create an image we reuse for each detect darknet_image = darknet.make_image(darknet.network_width(netMain), darknet.network_height(netMain), 3) # count the occurance of the cards cards_count = {} allocated = [] stack = {} frame_count = 0 while True: prev_time = time.time() ret, frame_read = cap.read() frame_rgb = cv2.cvtColor(frame_read, cv2.COLOR_BGR2RGB) frame_resized = cv2.resize( frame_rgb, (darknet.network_width(netMain), darknet.network_height(netMain)), interpolation=cv2.INTER_LINEAR) if frame_count % 5 == 0: frame_count = 1 # detect cards darknet.copy_image_from_bytes(darknet_image, frame_resized.tobytes()) detections = darknet.detect_image(netMain, metaMain, darknet_image, thresh=0.25) # draw bounding boxes image = cvDrawBoxes(detections, frame_resized) image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) # print(1/(time.time()-prev_time)) # compute clusters if (len(detections)) == 0: # reset() allocated = [] stack = {} # clusters = [] cards_count = {} for i in range(len(players)): players[i] = [] # 0 label, 1 accuracy, 2:0 x-coord, 2:1 y-coord, 2:2 frame width, 2:3 frame height for cards in detections: # Get the class of the detection temp = cards[0] accuracy = cards[1] * 100 suit_num = temp.decode("utf-8") # if a new card is found if accuracy >= acc_thresh: if suit_num not in stack: stack[suit_num] = [cards[2][0], cards[2][1]] cards_count[suit_num] = 1 else: cards_count[suit_num] += 1 if len( clusters ) != 0 and suit_num not in allocated and cards_count[ suit_num] > 5: # print("heyyyyy") idx = get_closest_centroid([cards[2][0], cards[2][1]], clusters) print(idx) players[idx].append(suit_num) allocated.append(suit_num) suggestions[idx] = suggestMove(players[idx], len(allocated)) new_clusters = getClusters(list(stack.values()), num_players) for new_cluster in new_clusters: clusters[get_closest_centroid( new_cluster, clusters)] = new_cluster # each player has to have at least two cards if (start and len(stack) >= num_players * 2): start = False clusters = getClusters(list(stack.values()), num_players) else: frame_count += 1 # suggestion displaying for i in range(len(players)): if len(clusters) > 0: print(i, players[i], suggestions[i], clusters[i]) temp = clusters[i] font = cv2.FONT_HERSHEY_SIMPLEX cord_x = int(temp[0]) cord_y = int(temp[1]) score = getScore(players[i]) if score > 0: cv2.putText(image, suggestions[i], (int(clusters[i][0]), int(clusters[i][1])), font, 2, (0, 0, 255), 2, cv2.LINE_AA) cv2.putText( image, 'Score: ' + str(score), (int(clusters[i][0]) - 100, int(clusters[i][1]) + 100), font, 2, (0, 0, 255), 2, cv2.LINE_AA) cv2.imshow('Demo', image) # out.write(image) # key command (q) for quit key = cv2.waitKey(1) & 0xFF if key == ord("q"): print(key) break # release video capture and close window cap.release() # out.release() cv2.destroyAllWindows()
def YOLO(configPath, weightPath, metaPath, inputPath): global metaMain, netMain, altNames if not os.path.exists(configPath): raise ValueError("Invalid config path `" + os.path.abspath(configPath) + "`") if not os.path.exists(weightPath): raise ValueError("Invalid weight path `" + os.path.abspath(weightPath) + "`") if not os.path.exists(metaPath): raise ValueError("Invalid data file path `" + os.path.abspath(metaPath) + "`") if netMain is None: netMain = darknet.load_net_custom(configPath.encode("ascii"), weightPath.encode("ascii"), 0, 1) # batch size = 1 if metaMain is None: metaMain = darknet.load_meta(metaPath.encode("ascii")) if altNames is None: try: with open(metaPath) as metaFH: metaContents = metaFH.read() import re match = re.search("names *= *(.*)$", metaContents, re.IGNORECASE | re.MULTILINE) if match: result = match.group(1) else: result = None try: if os.path.exists(result): with open(result) as namesFH: namesList = namesFH.read().strip().split("\n") altNames = [x.strip() for x in namesList] except TypeError: pass except Exception: pass darknet_image = darknet.make_image(darknet.network_width(netMain), darknet.network_height(netMain), 3) prev_time = time.time() nac = rg.read_nac(inputPath) height, width = nac.shape for y in range(0, height, R - OVER): for x in range(0, width, R - OVER): clp = rg.convert_to_uint8(nac[y:y + R, x:x + R]) image_resized = cv2.resize(clp, (darknet.network_width(netMain), darknet.network_height(netMain)), interpolation=cv2.INTER_LINEAR) darknet.copy_image_from_bytes(darknet_image, image_resized.tobytes()) detections = darknet.detect_image(netMain, metaMain, darknet_image, thresh=0.78) print(detections) if len(detections) > 0: image = cvDrawBoxes(detections, image_resized) image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) # cv2.imshow('Demo', image) name, ext = os.path.splitext(os.path.basename(inputPath)) cv2.imwrite('result/{}_{}_{}result.jpg'.format(name, x, y), image) cv2.waitKey() print(time.time() - prev_time)
import darknet as dn net = dn.load_net(b"../cfg/custom-tiny.cfg", b"../model/custom_tiny_yolov3.weights", 0) meta = dn.load_meta(b"../cfg/custom.data") r = dn.detect(net, meta, b"../data/processed_images/001/IMG_0897.jpg") print(r) print(dn.network_height(net)) print(dn.network_width(net))
def YOLO(): start = time.time() global metaMain, netMain, altNames configPath = "./cfg/yolov3-tiny.cfg" weightPath = "./bin/yolov3-tiny.weights" #configPath = "./cfg/yolov3.cfg" #weightPath = "./bin/yolov3.weights" metaPath = "./cfg/coco.data" if not os.path.exists(configPath): raise ValueError("Invalid config path `" + os.path.abspath(configPath) + "`") if not os.path.exists(weightPath): raise ValueError("Invalid weight path `" + os.path.abspath(weightPath) + "`") if not os.path.exists(metaPath): raise ValueError("Invalid data file path `" + os.path.abspath(metaPath) + "`") if netMain is None: netMain = darknet.load_net_custom(configPath.encode("ascii"), weightPath.encode("ascii"), 0, 1) # batch size = 1 if metaMain is None: metaMain = darknet.load_meta(metaPath.encode("ascii")) if altNames is None: try: with open(metaPath) as metaFH: metaContents = metaFH.read() import re match = re.search("names *= *(.*)$", metaContents, re.IGNORECASE | re.MULTILINE) if match: result = match.group(1) else: result = None try: if os.path.exists(result): with open(result) as namesFH: namesList = namesFH.read().strip().split("\n") altNames = [x.strip() for x in namesList] except TypeError: pass except Exception: pass #cap = cv2.VideoCapture(0) # JETBOT camera # cap = cv2.VideoCapture("nvarguscamerasrc ! video/x-raw(memory:NVMM), width=(int)1280, heigh=(int)720, format=(string)NV12, framerate=(fraction)24/1 ! nvvidconv ! video/x-raw, format=(string)BGRx ! videoconvert ! video/x-raw, format=(string)BGR ! appsink") # USB camera cap = cv2.VideoCapture( "v4l2src device=/dev/video1 ! video/x-raw, width=640, height=360, format=(string)YUY2,framerate=30/1 ! videoconvert ! video/x-raw,width=640,height=360,format=BGR ! appsink" ) # cap = cv2.VideoCapture("nvarguscamerasrc ! video/x-raw(memory:NVMM), width=(int)1280, height=(int)720, format=(string)NV12, framerate=(fraction)60/1 ! nvvidconv flip-method=2! video/x-raw, width=(int)1280, height=(int)720, format=(string)BGRx ! videoconvert ! video/x-raw, format=(string)BGR ! appsink") #cap = cv2.VideoCapture("/home/nvidia-tx2/OpenCV_in_Ubuntu/Data/Lane_Detection_Videos/solidWhiteRight.mp4") #cap = cv2.VideoCapture("nvcamerasrc ! video/x-raw(memory:NVMM), width=(int)640, height=(int)480, format=(string)I420, framerate=(fraction)30/1 ! nvvidconv flip-method=2 ! video/x-raw, format=(string)I420 ! videoconvert ! video/x-raw, format=(string)BGR ! appsink") #cap.set(3, 1280) #cap.set(4, 720) #out = cv2.VideoWriter( # "output.avi", cv2.VideoWriter_fourcc(*"MJPG"), 10.0, # (darknet.network_width(netMain), darknet.network_height(netMain))) print("Starting the YOLO loop...") # Create an image we reuse for each detect darknet_image = darknet.make_image(darknet.network_width(netMain), darknet.network_height(netMain), 3) while True: prev_time = time.time() ret, frame_read = cap.read() frame_rgb = cv2.cvtColor(frame_read, cv2.COLOR_BGR2RGB) frame_resized = cv2.resize( frame_rgb, (darknet.network_width(netMain), darknet.network_height(netMain)), interpolation=cv2.INTER_LINEAR) detections = darknet.detect_image(netMain, metaMain, frame_resized, thresh=0.25) if not detections: print("No objects") control.append(0) image = cvDrawBoxes(detections, frame_resized) image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) print(time.time() - prev_time) cv2.imshow('Demo', image) cv2.waitKey(3) if (time.time() - start > 500): robot.stop() quit() cap.release() out.release()
to_node("status", "Object detection started...") found_Objects = [] t = threading.Thread(target=check_stdin) t.start() netMain = darknet.load_net_custom(configPath.encode("ascii"), weightPath.encode("ascii"), 0, 1) # batch size = 1 metaMain = darknet.load_meta(metaPath.encode("ascii")) # Create an image we reuse for each detect darknet_image = darknet.make_image(darknet.network_width(netMain), darknet.network_height(netMain), 3) #raster for hand tracing.. here the image resolution horizontal_division = 36.0 vertical_division = 64.0 #detectionArray = np.zeros((int(vertical_division),int(horizontal_division),metaMain.classes),dtype=np.uint8) while True: detection_list = [] start_time = time.time() ret, frame = cap.read() if ret is False: continue
def YOLO(ifShowImg): loadNetWork() config = readConfigFile() # cap = cv2.VideoCapture(0) cap = cv2.VideoCapture( config.get('darknet_video_setting', "raspberry_video_address")) cap.set(3, config.getint('darknet_video_setting', "raspberry_video_width")) cap.set(4, config.getint('darknet_video_setting', "raspberry_video_height")) print("Starting the YOLO loop...") # Create an image we reuse for each detect darknet_image = darknet.make_image(darknet.network_width(netMain), darknet.network_height(netMain), 3) # 创建socket和web的传输 detected_result_to_raspberry_socket = socket.socket() # 创建 socket 对象 detected_result_to_raspberry_host = readConfigFile().get( 'darknet_video_setting', "detected_result_to_raspberry_host") # 获取本地主机名 detected_result_to_raspberry_port = readConfigFile().getint( 'darknet_video_setting', "detected_result_to_raspberry_port") # 设置端口 detected_result_to_raspberry_socket.bind( (detected_result_to_raspberry_host, detected_result_to_raspberry_port)) # 绑定端口 detected_result_to_raspberry_socket.listen(5) # 等待客户端连接 while True: # 这个循环的作用:循环读取摄像头信息,然后对其进行实时识别之后通过socket传给树莓派和浏览器控制端 # prev_time = time.time() # time.sleep(0.05) ret, frame_read = cap.read() frame_rgb = cv2.cvtColor(frame_read, cv2.COLOR_BGR2RGB) frame_resized = cv2.resize( frame_rgb, (darknet.network_width(netMain), darknet.network_height(netMain)), interpolation=cv2.INTER_LINEAR) darknet.copy_image_from_bytes(darknet_image, frame_resized.tobytes()) detections = darknet.detect_image(netMain, metaMain, darknet_image, thresh=0.25) # now = int(round(time.time() * 1000)) # send_message = f'[{now}]:{detections}' # print(send_message) if ifShowImg: image = cvDrawBoxes(detections, frame_resized) image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) # cv2.namedWindow('detected_result', cv2.WINDOW_NORMAL) # cv2.setWindowProperty('detected_result', cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN) cv2.imshow('detected_result', image) if cv2.waitKey(1) & 0xFF == ord('q'): break connection_to_raspberry, addr = detected_result_to_raspberry_socket.accept( ) # 建立和树莓派的socket通信 send_mess = bytes(str(detections), 'utf-8') msg = struct.pack('>I', len(send_mess)) + send_mess connection_to_raspberry.sendall(msg) # connection_to_raspberry.send(bytes(str(detections), 'utf-8')) connection_to_raspberry.close() # 关闭连接 cap.release()
def YOLO(configPath, weightPath, metaPath): global metaMain, netMain, altNames if not os.path.exists(configPath): raise ValueError("Invalid config path `" + os.path.abspath(configPath) + "`") if not os.path.exists(weightPath): raise ValueError("Invalid weight path `" + os.path.abspath(weightPath) + "`") if not os.path.exists(metaPath): raise ValueError("Invalid data file path `" + os.path.abspath(metaPath) + "`") if netMain is None: netMain = darknet.load_net_custom(configPath.encode("ascii"), weightPath.encode("ascii"), 0, 1) # batch size = 1 if metaMain is None: metaMain = darknet.load_meta(metaPath.encode("ascii")) if altNames is None: try: with open(metaPath) as metaFH: metaContents = metaFH.read() import re match = re.search("names *= *(.*)$", metaContents, re.IGNORECASE | re.MULTILINE) if match: result = match.group(1) else: result = None try: if os.path.exists(result): with open(result) as namesFH: namesList = namesFH.read().strip().split("\n") altNames = [x.strip() for x in namesList] except TypeError: pass except Exception: pass # Create an image we reuse for each detect darknet_image = darknet.make_image(darknet.network_width(netMain), darknet.network_height(netMain), 3) TEST_IMAGE_PATH = [] files = os.listdir(Path) for f in files: if f.endswith('jpg'): TEST_IMAGE_PATH.append(Path + f) for i, im in enumerate(TEST_IMAGE_PATH): frame_read = cv2.imread(im) frame_rgb = cv2.cvtColor(frame_read, cv2.COLOR_BGR2RGB) frame_resized = cv2.resize( frame_rgb, (darknet.network_width(netMain), darknet.network_height(netMain)), interpolation=cv2.INTER_LINEAR) darknet.copy_image_from_bytes(darknet_image, frame_resized.tobytes()) detections = darknet.detect_image(netMain, metaMain, darknet_image, thresh=0.5) image = cvDrawBoxes(detections, frame_resized) image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) cv2.imwrite('outImage{}.jpg'.format(i), image) print('outImage{}.jpg'.format(i))
def YOLO(imagepath): global metaMain, netMain, altNames configPath = "./configs/yolov4-helmet-detection.cfg" weightPath = "./configs/yolov4-helmet-detection.weights" metaPath = "./configs/yolov4-helmet-detection.data" if not os.path.exists(configPath): raise ValueError("Invalid config path `" + os.path.abspath(configPath) + "`") if not os.path.exists(weightPath): raise ValueError("Invalid weight path `" + os.path.abspath(weightPath) + "`") if not os.path.exists(metaPath): raise ValueError("Invalid data file path `" + os.path.abspath(metaPath) + "`") if netMain is None: netMain = darknet.load_net_custom(configPath.encode("ascii"), weightPath.encode("ascii"), 0, 1) # batch size = 1 if metaMain is None: metaMain = darknet.load_meta(metaPath.encode("ascii")) if altNames is None: try: with open(metaPath) as metaFH: metaContents = metaFH.read() import re match = re.search("names *= *(.*)$", metaContents, re.IGNORECASE | re.MULTILINE) if match: result = match.group(1) else: result = None try: if os.path.exists(result): with open(result) as namesFH: namesList = namesFH.read().strip().split("\n") altNames = [x.strip() for x in namesList] except TypeError: pass except Exception: pass if not os.path.exists("outputs"): os.mkdir("outputs") # load video file / streams image = cv2.imread(imagepath) print("Starting the YOLO loop...") # Create an image we reuse for each detect darknet_image = darknet.make_image(darknet.network_width(netMain), darknet.network_height(netMain), 3) # network image size (416*416, ...) network_image_size = (darknet.network_width(netMain), darknet.network_height(netMain)) image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) image_resized = cv2.resize(image_rgb, network_image_size, interpolation=cv2.INTER_LINEAR) darknet.copy_image_from_bytes(darknet_image, image_resized.tobytes()) detections = darknet.detect_image(netMain, metaMain, darknet_image, thresh=0.25) detections = resizeDetections(image.shape, network_image_size, detections) detect_image = cvDrawBoxes(detections, image_rgb) detect_image = cv2.cvtColor(detect_image, cv2.COLOR_BGR2RGB) cv2.imshow("detected", detect_image) cv2.imwrite("output.jpg", detect_image) while True: # press 'q' to quit if cv2.waitKey(1) == ord('q'): break cv2.destroyAllWindows()
def YOLO(): print("Starting YOLO...") # Make these global context global metaMain, netMain, altNames # Load our config configPath, metaPath, weightPath, cameraSource, FPS, threshold = loadConfig() # Instantiate our neural net for Darknet netMain = darknet.load_net_custom(configPath.encode( "ascii"), weightPath.encode("ascii"), 0, 1) # batch size = 1 # Load our meta data from external file metaMain = darknet.load_meta(metaPath.encode("ascii")) # Load our model labels try: with open(metaPath) as metaFH: print("Loading Models...") metaContents = metaFH.read() match = re.search("names *= *(.*)$", metaContents, re.IGNORECASE | re.MULTILINE) if match: result = match.group(1) else: result = None print("Loading Names List...") try: if os.path.exists(result): with open(result) as namesFH: namesList = namesFH.read().strip().split("\n") altNames = [x.strip() for x in namesList] except TypeError: pass except Exception: pass print("Successfully loaded Config") # Set up our camera capture capture = cv2.VideoCapture(cameraSource) if not capture: print("Invalid Capture Device Given, defaulting to 0") capture = cv2.VideoCapture(0) #Default down to 0 if not capture: print("No Capture Device found at given config or 0") raise ValueError("Invalid camera source") print("Starting Darknet capture...") # Create an image we reuse for each detect darknet_image = darknet.make_image(darknet.network_width(netMain), darknet.network_height(netMain), 3) nb_frame = FPS - 1 nb_sec = 0 while True: # Get the frame index nb_frame = (nb_frame + 1) % FPS print(f"starting loop: {nb_sec} seconds, frame idx: {nb_frame}") # Attempt to read a frame from the camera stream ret, frame_read = capture.read() # Only process the first frame of every second. if nb_frame != 0: continue else: nb_sec += 1 # If BBB demo, skip 50 first seconds if cameraSource == "/opt/darknet/test_video_files/BBB.mp4" and nb_sec < 50: continue if not ret: print("Could not read a frame, skipping...") continue # Read the frame and turn it into color frame_rgb = cv2.cvtColor(frame_read, cv2.COLOR_BGR2RGB) # Resize the frame correctly frame_resized = cv2.resize(frame_rgb, (darknet.network_width(netMain), darknet.network_height(netMain)), interpolation=cv2.INTER_LINEAR) # Translate into darknet format darknet.copy_image_from_bytes(darknet_image, frame_resized.tobytes()) # Detect on the image detections = darknet.detect_image(netMain, metaMain, darknet_image, thresh=threshold) # Draw boxes of the detections on the image image, detection_locations = cvDrawBoxes(detections, frame_resized) # Recolor the image image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) # Did we detect anything? if detection_locations: # This is sending a list of tuples of tuples, so to access you need to first access # the list with detection_locations[0], That will give you another tuple with two # entries, each a tuple # print(detection_locations) img = cv2.imencode(".png", image)[1] sendIOMessage(detection_locations, client, img) cv2.waitKey(3) capture.release()
def YOLO(): global metaMain, netMain, altNames configPath = "D:/darknet-master/build/darknet/x64/yolo-mytrain.cfg" weightPath = "D:/darknet-master/build/darknet/x64/yolo-mytrain_best.weights" metaPath = "D:/darknet-master/build/darknet/x64/data/mytrain.data" if not os.path.exists(configPath): raise ValueError("Invalid config path `" + os.path.abspath(configPath) + "`") if not os.path.exists(weightPath): raise ValueError("Invalid weight path `" + os.path.abspath(weightPath) + "`") if not os.path.exists(metaPath): raise ValueError("Invalid data file path `" + os.path.abspath(metaPath) + "`") if netMain is None: netMain = darknet.load_net_custom(configPath.encode("ascii"), weightPath.encode("ascii"), 0, 1) # batch size = 1 if metaMain is None: metaMain = darknet.load_meta(metaPath.encode("ascii")) if altNames is None: try: with open(metaPath) as metaFH: metaContents = metaFH.read() import re match = re.search("names *= *(.*)$", metaContents, re.IGNORECASE | re.MULTILINE) if match: result = match.group(1) else: result = None try: if os.path.exists(result): with open(result) as namesFH: namesList = namesFH.read().strip().split("\n") altNames = [x.strip() for x in namesList] except TypeError: pass except Exception: pass #cap = cv2.VideoCapture(0) cap = cv2.VideoCapture("D:/darknet-master/test3.mp4") cap.set(3, 1280) cap.set(4, 720) print("Starting the YOLO loop...") # Create an image we reuse for each detect darknet_image = darknet.make_image(darknet.network_width(netMain), darknet.network_height(netMain), 3) step = 0 test_id = 0 while True: step = step + 1 ret, frame_read = cap.read() frame_rgb = cv2.cvtColor(frame_read, cv2.COLOR_BGR2RGB) if step == MaxStep + 1: step = 0 if step == MaxStep: frame_resized = cv2.resize(frame_rgb, (darknet.network_width(netMain), darknet.network_height(netMain)), interpolation=cv2.INTER_LINEAR) darknet.copy_image_from_bytes(darknet_image, frame_resized.tobytes()) detections = darknet.detect_image(netMain, metaMain, darknet_image, thresh=0.25) ShowTheGoal(detections) result = cvRect(detections, frame_resized) if len(result) != 0: for i in range(len(result)): test_id += 1 cv2.imwrite( 'C:/Users/lenovo/Desktop/face/test3/' + str(test_id) + '.jpg', result[i]) cap.release()
""" t = threading.Thread(target=check_stdin) t.start() """ in case of shutdown """ signal.signal(signal.SIGINT, shutdown) #raster for hand tracing.. horizontal_division = 270.0 vertical_division = 480.0 to_node("status", "Gesture detection started...") darknet_image = darknet.make_image(darknet.network_width(netMain), darknet.network_height(netMain), 3) tracker_sort = {} last_detection_list = [] while True: start_time = time.time() FPS = global_FPS if FPS == 0: time.sleep(1) to_node("GESTURE_DET_FPS", float("{0:.2f}".format(0.0))) continue
def YOLO(): start = time.time() global metaMain, netMain, altNames configPath = "./cfg/yolov3-tiny.cfg" weightPath = "./bin/yolov3-tiny.weights" metaPath = "./cfg/coco.data" if not os.path.exists(configPath): raise ValueError("Invalid config path `" + os.path.abspath(configPath) + "`") if not os.path.exists(weightPath): raise ValueError("Invalid weight path `" + os.path.abspath(weightPath) + "`") if not os.path.exists(metaPath): raise ValueError("Invalid data file path `" + os.path.abspath(metaPath) + "`") if netMain is None: netMain = darknet.load_net_custom(configPath.encode("ascii"), weightPath.encode("ascii"), 0, 1) # batch size = 1 if metaMain is None: metaMain = darknet.load_meta(metaPath.encode("ascii")) if altNames is None: try: with open(metaPath) as metaFH: metaContents = metaFH.read() import re match = re.search("names *= *(.*)$", metaContents, re.IGNORECASE | re.MULTILINE) if match: result = match.group(1) else: result = None try: if os.path.exists(result): with open(result) as namesFH: namesList = namesFH.read().strip().split("\n") altNames = [x.strip() for x in namesList] except TypeError: pass except Exception: pass cap = cv2.VideoCapture( "v4l2src device=/dev/video1 ! video/x-raw, width=640, height=360, format=(string)YUY2,framerate=30/1 ! videoconvert ! video/x-raw,width=640,height=360,format=BGR ! appsink" ) print("Starting the YOLO loop...") # Create an image we reuse for each detect darknet_image = darknet.make_image(darknet.network_width(netMain), darknet.network_height(netMain), 3) for a in range(10): robot.forward(0.04 * a) time.sleep(0.2) while True: prev_time = time.time() ret, frame_read = cap.read() frame_rgb = cv2.cvtColor(frame_read, cv2.COLOR_BGR2RGB) frame_resized = cv2.resize( frame_rgb, (darknet.network_width(netMain), darknet.network_height(netMain)), interpolation=cv2.INTER_LINEAR) while len(control) >= 10: control.remove(control[0]) while len(signal) >= 2: signal.remove(signal[0]) detections = darknet.detect_image(netMain, metaMain, frame_resized, thresh=0.25) det = False for d in detections: if d[0].decode() == "person": det = True print("####################\n") print(control) if not detections or not det: control.append(0) print("No Human") elif det: print("Human") image = cvDrawBoxes(detections, frame_resized)[0] xm = cvDrawBoxes(detections, frame_resized)[1] signalcontrol(xm) image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) print("Time: " + str(round(time.time() - prev_time, 4))) print("\n####################") cv2.imshow('Demo', image) cv2.waitKey(3) if (time.time() - start > 500): robot.stop() quit() cap.release() out.release()
yolo_fail = True return [0, 0, 0, 0], yolo_fail if __name__ == "__main__": YOLO() tracker = PYSOTINIT() width = 1280 height = 720 cap = cv2.VideoCapture(vidName) print("Starting the YOLO loop...") darknet_image = darknet.make_image(darknet.network_width(netMain), darknet.network_height(netMain), 3) prev_bbox = [0, 0, 0, 0] cur_bbox = [0, 0, 0, 0] cur_delta_bbox = 0 alarmCnt = 0 frame_number = 0 out_of_center = False side_car_enter = False l_lines = [] r_lines = [] left_fit_line = [] right_fit_line = [] center_x = None
try: if os.path.exists(result): with open(result) as namesFH: namesList = namesFH.read().strip().split("\n") altNames = [x.strip() for x in namesList] except TypeError: pass except Exception: pass image_name = '/home/weizy/Programs/YOLOv4/darknet/data/dog.jpg' src_img = cv2.imread(image_name) bgr_img = src_img[:, :, ::-1] height, width = bgr_img.shape[:2] rsz_img = cv2.resize( bgr_img, (darknet.network_width(netMain), darknet.network_height(netMain)), interpolation=cv2.INTER_LINEAR) darknet_image, _ = darknet.array_to_image(rsz_img) detections = darknet.detect_image(netMain, metaMain, darknet_image, thresh=0.25) # convert xywh to xyxy def convert_back(x, y, w, h): xmin = int(round(x - (w / 2))) xmax = int(round(x + (w / 2))) ymin = int(round(y - (h / 2))) ymax = int(round(y + (h / 2))) return xmin, ymin, xmax, ymax
def YOLO(): global metaMain, netMain, altNames configPath = "yolov3-tiny-obj.cfg" weightPath = "yolov3-tiny-obj_3000.weights" metaPath = "obj.data" if not os.path.exists(configPath): raise ValueError("Invalid config path `" + os.path.abspath(configPath) + "`") if not os.path.exists(weightPath): raise ValueError("Invalid weight path `" + os.path.abspath(weightPath) + "`") if not os.path.exists(metaPath): raise ValueError("Invalid data file path `" + os.path.abspath(metaPath) + "`") if netMain is None: netMain = darknet.load_net_custom(configPath.encode("ascii"), weightPath.encode("ascii"), 0, 1) # batch size = 1 if metaMain is None: metaMain = darknet.load_meta(metaPath.encode("ascii")) if altNames is None: try: with open(metaPath) as metaFH: metaContents = metaFH.read() import re match = re.search("names *= *(.*)$", metaContents, re.IGNORECASE | re.MULTILINE) if match: result = match.group(1) else: result = None try: if os.path.exists(result): with open(result) as namesFH: namesList = namesFH.read().strip().split("\n") altNames = [x.strip() for x in namesList] except TypeError: pass except Exception: pass #cap = cv2.VideoCapture(0) # Uncomment this if you want to run it on your webcam #cap = cv2.VideoCapture(0, cv2.CAP_DSHOW) cap = cv2.VideoCapture( "MOV_2400.mp4" ) # Uncomment this if you want to run it on a video. Change the path to the video first. # cap.set(3, 1280) # cap.set(4, 720) vid_cod = cv2.VideoWriter_fourcc(*'XVID') out = cv2.VideoWriter( "cam_video.avi", vid_cod, 10.0, (darknet.network_width(netMain), darknet.network_height(netMain))) print("Starting the YOLO loop...") # Create an image we reuse for each detect darknet_image = darknet.make_image(darknet.network_width(netMain), darknet.network_height(netMain), 3) while True: ret, frame_read = cap.read() frame_rgb = cv2.cvtColor(frame_read, cv2.COLOR_BGR2RGB) frame_resized = cv2.resize( frame_rgb, (darknet.network_width(netMain), darknet.network_height(netMain)), interpolation=cv2.INTER_LINEAR) gray = cv2.cvtColor(frame_read, cv2.COLOR_BGR2GRAY) gray_resized = cv2.resize( gray, (darknet.network_width(netMain), darknet.network_height(netMain)), interpolation=cv2.INTER_LINEAR) darknet.copy_image_from_bytes(darknet_image, frame_resized.tobytes()) detections = darknet.detect_image(netMain, metaMain, darknet_image, thresh=0.25) rects = detector.detectMultiScale(gray_resized, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30), flags=cv2.CASCADE_SCALE_IMAGE) if len(rects) > 0: for (fX, fY, fW, fH) in rects: roi = gray_resized[fY:fY + fH, fX:fX + fW] roi = cv2.resize(roi, (48, 48)) roi = roi.astype("float") / 255.0 roi = img_to_array(roi) roi = np.expand_dims(roi, axis=0) # make a prediction on the ROI, then lookup the class label preds = FER_model.predict(roi)[0] label = Emotion_classes[preds.argmax()] # loop over the labels + probabilities and draw them for (i, (emotion, prob)) in enumerate(zip(Emotion_classes, preds)): # construct the label text text = "{}: {:.2f}%".format(emotion, prob * 100) # draw the label + probability bar on the canvas # w = int(prob * 300) cv2.putText(frame_resized, text, (fX, fY - (20 * i) - 25), cv2.FONT_HERSHEY_SIMPLEX, 0.45, (255, 255, 255), 2) cv2.putText(frame_resized, label, (fX, fY - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.45, (255, 0, 0), 2) cv2.rectangle(frame_resized, (fX, fY), (fX + fW, fY + fH), (255, 0, 0), 2) image = cardsDrawBoxes(detections, frame_resized, card_model) image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) cv2.imshow('Demo', image) out.write(image) if cv2.waitKey(1) & 0xFF == ord("q"): break cap.release() out.release() cv2.destroyAllWindows()
def YOLO(videoPath="./data/videos/car-accident-3.mp4", configPath="./cfg/yolov3.cfg", weightPath="./yolov3.weights", metaPath="./cfg/coco-new.data", screenRecord=False): try: validateYOLO(configPath, weightPath, metaPath) except Exception: print("Error Happened in ValidateYOLO") return centroidTracker = CentroidTracker(50) # Select a video element to apply YOLO cap = cv2.VideoCapture(videoPath) if screenRecord: cap = cv2.VideoCapture(0) cap.set(3, 1280) cap.set(4, 720) # Write YOLO results to an avi video file out = cv2.VideoWriter( "output.avi", cv2.VideoWriter_fourcc(*"MJPG"), 10.0, (darknet.network_width(netMain), darknet.network_height(netMain))) # Create an image we reuse for each detect darknet_image = darknet.make_image(darknet.network_width(netMain), darknet.network_height(netMain), 3) # Prepare video print("Starting the YOLO loop...") while True: prev_time = time.time() ret, frame_read = cap.read() frame_rgb = cv2.cvtColor(frame_read, cv2.COLOR_BGR2RGB) # Resized borderless video frame frame_resized = cv2.resize( frame_rgb, (darknet.network_width(netMain), darknet.network_height(netMain)), interpolation=cv2.INTER_LINEAR) darknet.copy_image_from_bytes(darknet_image, frame_resized.tobytes()) # We have now detections dictionary detections = darknet.detect_image(netMain, metaMain, darknet_image, thresh=0.25) # Inside app.py # Main App Function useDetections(detections, frame_resized, centroidTracker) # print(1 / (time.time() - prev_time)) # If 'esc' or 'q' key pressed break the loop k = cv2.waitKey(2) & 0xFF if k == 27 or k == ord('q'): break cv2.destroyAllWindows() cap.release() out.release()
def __init__(self, cam_num, parent=None): self.cam_num = cam_num #載入資料庫的資料 self.reload() QtWidgets.QMainWindow.__init__(self) #self.setWindowTitle("ggez") self.ui = uic.loadUi("webcam.ui", self) #self.ui.setFixedSize(self.size()) self.ui.tabWidget.setTabText(0, "Main") self.ui.tabWidget.setTabText(1, "Search") self.ui.tabWidget.setTabText(2, "Setting") self.ui.Open_Button.clicked.connect(self.open_detect) self.ui.pushButton.clicked.connect(self.save_image) self.ui.Stop_Button.clicked.connect(self.stop) self.ui.lineEdit.returnPressed.connect(self.save_image) self.ui.Send_Button.clicked.connect(self.search_event) self.ui.lineEdit_2.returnPressed.connect(self.search_event) #由資料庫抓取資料 self.ui.reload_image.clicked.connect(self.reload) self.ui.Recognition_checkbox.clicked.connect( self.Recognition_check_event) self.Recognition_checkbox.setStyleSheet( "QPushButton{background:black;border-radius:20;}") self.widget.setStyleSheet( "QWidget{border-width:2px;}" "QWidget{border-color:black;}" "QWidget{border-style:outset;}" "QWidget{height:100;}" "QWidget{border-radius:5px;}" "QWidget{background-color:qlineargradient(x1 : 0, y1 : 0, x2 : 0, y2 : 1, stop : 0.0 #f5f9ff,stop : 0.5 #c7dfff,stop : 0.55 #afd2ff,stop : 1.0 #c0dbff);}" ) self.ui.close_button.clicked.connect(self.close_camera) self.ui.tabWidget.setTabIcon(0, QtGui.QIcon("home.png")) self.ui.tabWidget.setIconSize(QtCore.QSize(30, 30)) self.ui.tabWidget.setTabIcon(1, QtGui.QIcon("search.png")) self.ui.tabWidget.setIconSize(QtCore.QSize(30, 30)) self.ui.tabWidget.setTabIcon(2, QtGui.QIcon("setting.png")) self.ui.tabWidget.setIconSize(QtCore.QSize(30, 30)) self.setWindowIcon(QtGui.QIcon("window_icon.png")) tab_shape = QtWidgets.QTabWidget.Triangular self.tabWidget.setTabShape(tab_shape) self.check = 0 self.i = 0 self.in_or_out = 0 self.Recognition_check = False self.label2 = QLabel(self) self.label1 = QLabel(self) self.label1.setStyleSheet( "QLabel{background:0;}" "QLabel{color:rgb(300,300,300,120);font-size:30px;font-weight:bold;font-family:宋体;}" ) # 動態顯示時間在label上 timer = QTimer(self) timer.timeout.connect(self.showtime) timer.start() self.label1.setAlignment(QtCore.Qt.AlignCenter) self.label2.setGeometry(self.label.width() + 20, 150, 45, 45) #self.label1.resize(self.width(),50) # In[] #----------------口罩辨識初始化----------------- self.frame_queue = Queue() self.darknet_image_queue = Queue(maxsize=1) self.detections_queue = Queue(maxsize=1) self.fps_queue = Queue(maxsize=1) #宣告一個放照片的Queue self.YOLO_image_queue = Queue(maxsize=1) #宣告一個放人臉的Queue self.YOLO_face_queue = Queue(maxsize=1) self.face = 0 self.recorded_people = [] self.clock = 0 self.args = parser() check_arguments_errors(self.args) self.network, self.class_names, self.class_colors = darknet.load_network( self.args.config_file, self.args.data_file, self.args.weights, batch_size=1) # Darknet doesn't accept numpy images. # Create one with image we reuse for each detect self.d_width = darknet.network_width(self.network) self.d_height = darknet.network_height(self.network) self.darknet_image = darknet.make_image(self.d_width, self.d_height, 3) self.show()
def YOLO(args): global metaMain, netMain, altNames configPath = "./cfg/yolov4.cfg" weightPath = "./yolov4.weights" metaPath = "./cfg/coco.data" if not os.path.exists(configPath): raise ValueError("Invalid config path `" + os.path.abspath(configPath) + "`") if not os.path.exists(weightPath): raise ValueError("Invalid weight path `" + os.path.abspath(weightPath) + "`") if not os.path.exists(metaPath): raise ValueError("Invalid data file path `" + os.path.abspath(metaPath) + "`") if netMain is None: netMain = darknet.load_net_custom(configPath.encode("ascii"), weightPath.encode("ascii"), 0, 1) # batch size = 1 if metaMain is None: metaMain = darknet.load_meta(metaPath.encode("ascii")) if altNames is None: try: with open(metaPath) as metaFH: metaContents = metaFH.read() import re match = re.search("names *= *(.*)$", metaContents, re.IGNORECASE | re.MULTILINE) if match: result = match.group(1) else: result = None try: if os.path.exists(result): with open(result) as namesFH: namesList = namesFH.read().strip().split("\n") altNames = [x.strip() for x in namesList] except TypeError: pass except Exception: pass # Create an image we reuse for each detect darknet_image = darknet.make_image(darknet.network_width(netMain), darknet.network_height(netMain), 3) #cap = cv2.VideoCapture(0) cap = cv2.VideoCapture(args.input) cap.set(3, 1280) cap.set(4, 720) #out = cv2.VideoWriter(args.output, cv2.VideoWriter_fourcc(*"mp4v"), 30,(1280, 720)) print("Starting the YOLO loop...") print("width =", darknet.network_width(netMain), "height=", darknet.network_height(netMain)) model_w = darknet.network_width(netMain) model_h = darknet.network_height(netMain) ret, frame_read = cap.read() frame_count = 0 frame_h, frame_w, _ = frame_read.shape scale_h = frame_h / model_h scale_w = frame_w / model_w scaling_factor = max(scale_h, scale_w) resize_dim = (round(frame_w / scaling_factor), round(frame_h / scaling_factor)) out = cv2.VideoWriter(args.output, cv2.VideoWriter_fourcc(*"mp4v"), 30, resize_dim) start_time = time.time() while ret: prev_time = time.time() frame_rgb = cv2.cvtColor(frame_read, cv2.COLOR_BGR2RGB) # changing interpolation from cv2.INTER_LINEAR to cv2.INTER_AREA reduces fps by 1 frame_resized = cv2.resize(frame_rgb, resize_dim, interpolation=cv2.INTER_AREA) frame_resized = unsharp_mask(frame_resized) frame_resized = cv2.copyMakeBorder(frame_resized, 0, model_h - resize_dim[1], 0, 0, cv2.BORDER_CONSTANT) darknet.copy_image_from_bytes(darknet_image, frame_resized.tobytes()) detections = darknet.detect_image(netMain, metaMain, darknet_image, thresh=0.3) persons = [i for i in detections if i[0].decode('ASCII') == 'person'] persons = distance.validate_area(persons, resize_dim) motorbikes = [ i for i in detections if i[0].decode('ASCII') in ('motorbike', 'bicycle') ] motorbikes = distance.validate_area(motorbikes, resize_dim) #print(detections) if motorbikes: motorbikes, persons = distance.combine(persons, motorbikes) image = cvDrawBoxes(persons, frame_resized) image = cvDrawBoxes(motorbikes, image, 'red') # violations,non_violations = distance.calc_distance(persons,darknet.network_height(netMain)) # # image = cvDrawBoxes(violations, frame_resized, colour = "red") # image = cvDrawBoxes(non_violations, frame_resized) image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) image = image[:resize_dim[1], :] #image = cv2.resize(image,(frame_w,frame_h), interpolation=cv2.INTER_LINEAR) out.write(image) print(round(1 / (time.time() - prev_time), 1)) frame_count += 1 ret, frame_read = cap.read() cap.release() out.release() print('Average fps: ', 1 / ((time.time() - start_time) / frame_count))
def YOLO(F=0.00415, sd=0, video_path='/content/mask_footage.mp4', configPath="cfg/custom-yolov4-detector.cfg", weightPath="/content/custom-yolov4-detector_best.weights", metaPath="data/obj.data"): global metaMain, netMain, altNames '''configPath = "./cfg/yolov4.cfg" weightPath = "./yolov4.weights" metaPath = "./cfg/coco.data"''' SD = sd sensor_w = 4.8 f = F if not os.path.exists(configPath): raise ValueError("Invalid config path `" + os.path.abspath(configPath) + "`") if not os.path.exists(weightPath): raise ValueError("Invalid weight path `" + os.path.abspath(weightPath) + "`") if not os.path.exists(metaPath): raise ValueError("Invalid data file path `" + os.path.abspath(metaPath) + "`") if netMain is None: netMain = darknet.load_net_custom(configPath.encode("ascii"), weightPath.encode("ascii"), 0, 1) # batch size = 1 if metaMain is None: metaMain = darknet.load_meta(metaPath.encode("ascii")) if altNames is None: try: print("DF") with open(metaPath) as metaFH: metaContents = metaFH.read() import re match = re.search("names *= *(.*)$", metaContents, re.IGNORECASE | re.MULTILINE) if match: result = match.group(1) else: result = None try: if os.path.exists(result): with open(result) as namesFH: namesList = namesFH.read().strip().split("\n") altNames = [x.strip() for x in namesList] except TypeError: pass except Exception: pass #cap = cv2.VideoCapture(0) cap = cv2.VideoCapture(video_path) cap.set(3, 1280) cap.set(4, 720) fps = 30.0 out = cv2.VideoWriter( "output.avi", cv2.VideoWriter_fourcc(*"MJPG"), fps, (darknet.network_width(netMain), darknet.network_height(netMain))) print("Starting the YOLO loop...") w = darknet.network_width(netMain) f = f * 1000 * 512 / sensor_w print(f, SD) # Create an image we reuse for each detect darknet_image = darknet.make_image(darknet.network_width(netMain), darknet.network_height(netMain), 3) while True: #try: prev_time = time.time() ret, frame_read = cap.read() frame_rgb = cv2.cvtColor(frame_read, cv2.COLOR_BGR2RGB) frame_resized = cv2.resize(frame_rgb, (699, 388)) darknet.copy_image_from_bytes(darknet_image, frame_resized.tobytes()) detections = darknet.detect_image(netMain, metaMain, darknet_image, thresh=0.25) image = cvDrawBoxes(detections, frame_resized, SD, f) image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) out.write(image) print(1 / (time.time() - prev_time)) io.imshow(image) io.show() cv2.waitKey(3) #except: #break; cap.release() out.release()
def dedect_and_post(source, destiantion="print", configPath="./cfg/yolov3.cfg", weightPath="./yolov3.weights", metaPath="./cfg/coco.data"): """source = ip to stream or path to video file destination = "print" or ip where to post the json detection object""" # Test if all paths exist for path in [source, configPath, weightPath, metaPath]: path_exsists = os.path.exists(path) print(path, path_exsists) # Stop process if file is missing if not path_exsists: quit() netMain = darknet.load_net_custom(configPath.encode("ascii"), weightPath.encode("ascii"), 0, 1) # batch size = 1 metaMain = darknet.load_meta(metaPath.encode("ascii")) width = int(darknet.network_width(netMain)) height = int(darknet.network_height(netMain)) print("Detection frame size") print(width, height) darknet_image = darknet.make_image(int(width), int(height), 3) # Build darkent image cap = cv2.VideoCapture(source) # Open stream or file original_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) # float original_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) # float original_FPS = cap.get(cv2.CAP_PROP_FPS) global k_width global k_height print(width, original_width) k_width = float(width) / float(original_width) k_height = float(height) / float(original_height) print(k_width, k_height) print("Stream/file frame size") print(original_width, original_height) # Check if camera opened successfully if (cap.isOpened() == False): print("Error opening video stream or file") start_time = datetime.datetime.utcnow() detections_dict = {} detections_dict["meta"] = dict( height=original_height, width=original_width, source=source, stream_start_utc=start_time.isoformat(), FPS=original_FPS, #k_width = k_width, #k_height = k_height ) frame_number = 0 # Read until video is completed while (cap.isOpened()): # Capture frame-by-frame ret, frame = cap.read() if ret == True: frame_resized = cv2.resize( frame, (int(width), int(height)), interpolation=cv2.INTER_LINEAR) # Resize frame to fit darknet darknet.copy_image_from_bytes(darknet_image, frame_resized.tobytes()) detections = darknet.detect_image(netMain, metaMain, darknet_image, thresh=0.25) #print(detections) detections_dict["meta"][ "frame_timestamp_utc"] = datetime.datetime.utcnow().isoformat( ) detections_dict["meta"]["frame_number"] = frame_number detections_dict["meta"]["frame_UUID"] = str(uuid4()) meta_json = detections_to_json(detections, detections_dict) # send to destination or print the dedection result if destiantion == "print": print(meta_json) else: reponse = requests.post(destiantion, json=meta_json) print(reponse) # image = cvDrawBoxes(detections, frame) # Display the resulting frame #cv2.imshow('Frame', image) # Press Q on keyboard to exitq if cv2.waitKey(1) & 0xFF == ord('q'): break frame_number += 1 # Break the loop else: break # When everything done, release the video capture object cap.release() # Closes all the frames cv2.destroyAllWindows()
def main(self): # cap = cv2.VideoCapture(1) cap = cv2.VideoCapture('t1.avi') # cap = cv2.VideoCapture('closer_school.mp4') cap.set( 3, WEIGHT ) # Feed resolution from webcam to YOLO however output is from yolo cfg file resolution cap.set(4, HEIGHT) out = cv2.VideoWriter( "output.avi", cv2.VideoWriter_fourcc(*"MJPG"), 10.0, (darknet.network_width(netMain), darknet.network_height(netMain))) print("Starting the YOLO loop...") # Create an image we reuse for each detect darknet_image = darknet.make_image(darknet.network_width(netMain), darknet.network_height(netMain), 3) # Variables frame_count = 0 mx = frame_show = '' self.mission = '' while True: prev_time = time.time() ret, frame_read = cap.read() frame_read = cv2.resize(frame_read, (HEIGHT, WEIGHT), cv2.INTER_AREA) hsv = cv2.cvtColor(frame_read, cv2.COLOR_BGR2HSV) h, s, v = cv2.split(hsv) mean_brt = np.mean(v) # Brightness Correction if mean_brt > BRT_THRESH: frame_read = np.int16(frame_read) frame_read = frame_read * (contrast / 127 + 1) - contrast + brightness frame_read = np.clip(frame_read, 0, 255) frame_read = np.uint8(frame_read) frame_rgb = cv2.cvtColor(frame_read, cv2.COLOR_BGR2RGB) frame_resized = cv2.resize( frame_rgb, (darknet.network_width(netMain), darknet.network_height(netMain)), interpolation=cv2.INTER_LINEAR) # CHECK RESIZING KEEPING RATIO darknet.copy_image_from_bytes(darknet_image, frame_resized.tobytes()) detections = darknet.detect_image(netMain, metaMain, darknet_image, THRESH) # Uses YOLO to detect detect = self.checkSize( detections) # Gets rid of improper detections fps = (1 / (time.time() - prev_time)) image = self.cvDrawBoxes(detect, frame_resized) image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) decoded = [] if detect: sg1 = detect[0][0].decode() if len(detect) > 1: for i in range(len(detect)): decoded.append(detect[i][0].decode()) if 'red' in decoded: index = decoded.index('red') elif 'green' in decoded: index = decoded.index('green') elif 'u-turn' in decoded: index = decoded.index('u-turn') else: index = 0 sg1 = decoded[index] # prec1 = detect[0][1] self.sign[sg1][0] += 1 # self.sign[sg1][1] += prec1 print(self.sign) mx = max(self.sign, key=lambda key: self.sign[key][0]) if self.sign[mx][0]: frame_show = str(self.sign[mx][0]) frame_count = 0 else: frame_count += 1 # In order to know IDLE, count frames without any reading and store in frame_count self.high_vals = { k: v for (k, v) in self.sign.items() if v[0] >= SIZE } # Check if there are any values above size limit if self.high_vals: # If there is any sign > SIZE max_sign = max( self.high_vals, key=lambda key: self.high_vals[key][0] ) # Finds the key with highest frame_count on the dictionary self.mission = max_sign self.reset_dict( ) # 0 = reset all 1 = reset all but left right inter green elif frame_count > IDLE_SIZE: # If No detection for the last IDLE_SIZE frames -> refresh all (used to avoid random uncertain and uncontinuos detections) self.reset_dict() mx = 'SEARCHING' frame_show = '' frame_count = 0 self.mission = 'default' print(self.yolo_mission()) # DEBUGING: write frame count, last mission, the sign with max count and its count, fps cv2.putText(image, str(frame_count), bottomLeftCornerOfText, font, fontScale, fontColor, lineType) cv2.putText(image, mx, (400, 570), font, fontScale, fontColor, lineType) cv2.putText(image, frame_show, (550, 570), font, fontScale, fontColor, lineType) cv2.putText(image, 'Mission: ' + self.mission, (10, 400), font, fontScale, fontColor, lineType) cv2.putText(image, 'fps:' + str(int(fps)), (550, 30), font, 0.5, fontColor, lineType) cv2.rectangle(image, (0, 0), (10, 10), (60, 60, 60), 1) cv2.rectangle(image, (0, 0), (316, 316), (60, 60, 60), 1) out.write(image) # P cv2.imshow('YOLO', image) cv2.waitKey(3)
def YOLO(): global metaMain, netMain, altNames configPath = "./cfg/yolov3.cfg" weightPath = "./yolov3.weights" metaPath = "./cfg/coco.data" if not os.path.exists(configPath): raise ValueError("Invalid config path `" + os.path.abspath(configPath) + "`") if not os.path.exists(weightPath): raise ValueError("Invalid weight path `" + os.path.abspath(weightPath) + "`") if not os.path.exists(metaPath): raise ValueError("Invalid data file path `" + os.path.abspath(metaPath) + "`") if netMain is None: netMain = darknet.load_net_custom(configPath.encode("ascii"), weightPath.encode("ascii"), 0, 1) # batch size = 1 if metaMain is None: metaMain = darknet.load_meta(metaPath.encode("ascii")) if altNames is None: try: with open(metaPath) as metaFH: metaContents = metaFH.read() import re match = re.search("names *= *(.*)$", metaContents, re.IGNORECASE | re.MULTILINE) if match: result = match.group(1) else: result = None try: if os.path.exists(result): with open(result) as namesFH: namesList = namesFH.read().strip().split("\n") altNames = [x.strip() for x in namesList] except TypeError: pass except Exception: pass cap = cv2.VideoCapture("/home/mustafa/TownCentreXVID.avi") height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT) width = cap.get(cv2.CAP_PROP_FRAME_WIDTH) ff = cv2.VideoWriter_fourcc(*"MP4V") out = cv2.VideoWriter("output.avi", ff, 20.0, (832, 416)) out1 = cv2.VideoWriter("output25.avi", ff, 20.0, (416, 416)) print("Starting the YOLO loop...") # Create an image we reuse for each detect darknet_image = darknet.make_image(darknet.network_width(netMain), darknet.network_height(netMain), 3) while True: prev_time = time.time() ret, frame_read = cap.read() frame_rgb = cv2.cvtColor(frame_read, cv2.COLOR_BGR2RGB) frame_resized = cv2.resize( frame_rgb, (darknet.network_width(netMain), darknet.network_height(netMain)), interpolation=cv2.INTER_LINEAR) darknet.copy_image_from_bytes(darknet_image, frame_resized.tobytes()) detections = darknet.detect_image(netMain, metaMain, darknet_image, thresh=0.90) slice1 = np.zeros((int(416), int(416), 3)) bg = np.uint8(slice1) image = cvDrawBoxes(detections, frame_resized, bg) image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) # print(1/(time.time()-prev_time)) numpy_vertical_concat = np.concatenate((image, bg), axis=1) out.write(numpy_vertical_concat) # out1.write(bg) cv2.imshow('Numpy Vertical', numpy_vertical_concat) cv2.imshow('Demo', image) cv2.imshow("draw", bg) cv2.waitKey(3) cap.release() out.release()
def loop_and_detect(stream_handler, conf_th): """Loop, grab images from camera, and do object detection. # Arguments stream_handler: the stream handler object. conf_th: confidence/score threshold for object detection. """ show_fps = True full_scrn = True fps = 0.0 netMain = darknet.load_net_custom(configPath.encode("ascii"), weightPath.encode("ascii"), 0, 1) # batch size = 1 metaMain = darknet.load_meta(metaPath.encode("ascii")) tic = time.time() img = stream_handler.read_streams() darknet_image = darknet.make_image(darknet.network_width(netMain), darknet.network_height(netMain), 3) scale=(float(img.shape[1])/darknet.network_width(netMain),\ float(img.shape[0])/darknet.network_height(netMain)) while True: if cv2.getWindowProperty(WINDOW_NAME, 0) < 0: # Check to see if the user has closed the display window. # If yes, terminate the while loop. break frame_read = stream_handler.read_streams() frame_rgb = frame_read[:, :, ::-1] frame_resized = cv2.resize( frame_rgb, (darknet.network_width(netMain), darknet.network_height(netMain)), interpolation=cv2.INTER_LINEAR) darknet.copy_image_from_bytes(darknet_image, frame_resized.tobytes()) detections = darknet.detect_image(netMain, metaMain, darknet_image, thresh=conf_th) img = cvDrawBoxes(detections, frame_read, scale) if show_fps: img = draw_help_and_fps(img, fps) cv2.imshow(WINDOW_NAME, img) toc = time.time() curr_fps = 1.0 / (toc - tic) # calculate an exponentially decaying average of fps number fps = curr_fps if fps == 0.0 else (fps * 0.9 + curr_fps * 0.1) tic = toc key = cv2.waitKey(1) if key == ord('q') or key == ord('Q'): # q key: quit program break elif key == ord('H') or key == ord('h'): # Toggle help/fps show_fps = not show_fps elif key == ord('F') or key == ord('f'): # Toggle fullscreen full_scrn = not full_scrn set_full_screen(full_scrn)
def YOLO(): global metaMain, netMain, altNames configPath = "./yolo-obj.cfg" weightPath = "./yolo-obj_last.weights" metaPath = "./data/obj.data" if not os.path.exists(configPath): raise ValueError("Invalid config path `" + os.path.abspath(configPath) + "`") if not os.path.exists(weightPath): raise ValueError("Invalid weight path `" + os.path.abspath(weightPath) + "`") if not os.path.exists(metaPath): raise ValueError("Invalid data file path `" + os.path.abspath(metaPath) + "`") if netMain is None: netMain = darknet.load_net_custom(configPath.encode("ascii"), weightPath.encode("ascii"), 0, 1) # batch size = 1 if metaMain is None: metaMain = darknet.load_meta(metaPath.encode("ascii")) if altNames is None: try: with open(metaPath) as metaFH: metaContents = metaFH.read() import re match = re.search("names *= *(.*)$", metaContents, re.IGNORECASE | re.MULTILINE) if match: result = match.group(1) else: result = None try: if os.path.exists(result): with open(result) as namesFH: namesList = namesFH.read().strip().split("\n") altNames = [x.strip() for x in namesList] except TypeError: pass except Exception: pass #cap = cv2.VideoCapture(0) image_name = parsed_args.input ext = image_name.split('.') # Create an image we reuse for each detect darknet_image = darknet.make_image(darknet.network_width(netMain), darknet.network_height(netMain), 3) frame_read = cv2.imread(image_name) frame_rgb = cv2.cvtColor(frame_read, cv2.COLOR_BGR2RGB) frame_resized = cv2.resize( frame_rgb, (darknet.network_width(netMain), darknet.network_height(netMain)), interpolation=cv2.INTER_LINEAR) darknet.copy_image_from_bytes(darknet_image, frame_resized.tobytes()) detections = darknet.detect_image(netMain, metaMain, darknet_image, thresh=0.25) cvDrawBoxes(detections, frame_resized, image_name) darknet_image = None frame_read = None frame_rgb = None frame_resized = None detections = None
def YOLO(): global metaMain, netMain, altNames configPath = "./cfg/yolov3.cfg" weightPath = "./yolov3.weights" metaPath = "./cfg/coco.data" if not os.path.exists(configPath): raise ValueError("Invalid config path `" + os.path.abspath(configPath) + "`") if not os.path.exists(weightPath): raise ValueError("Invalid weight path `" + os.path.abspath(weightPath) + "`") if not os.path.exists(metaPath): raise ValueError("Invalid data file path `" + os.path.abspath(metaPath) + "`") if netMain is None: netMain = darknet.load_net_custom(configPath.encode("ascii"), weightPath.encode("ascii"), 0, 1) # batch size = 1 if metaMain is None: metaMain = darknet.load_meta(metaPath.encode("ascii")) if altNames is None: try: with open(metaPath) as metaFH: metaContents = metaFH.read() import re match = re.search("names *= *(.*)$", metaContents, re.IGNORECASE | re.MULTILINE) if match: result = match.group(1) else: result = None try: if os.path.exists(result): with open(result) as namesFH: namesList = namesFH.read().strip().split("\n") altNames = [x.strip() for x in namesList] except TypeError: pass except Exception: pass #cap = cv2.VideoCapture(0) cap = cv2.VideoCapture( "nvcamerasrc ! video/x-raw(memory:NVMM), width=(int)1280, height=(int)720,\ format=(string)I420, framerate=(fraction)30/1 ! nvvidconv flip-method=2 ! video/x-raw, \ format=(string)BGRx ! videoconvert ! video/x-raw, format=(string)BGR ! appsink" ) cap.set(3, 1280) cap.set(4, 720) # out = cv2.VideoWriter( # "output.avi", cv2.VideoWriter_fourcc(*"MJPG"), 10.0, # (darknet.network_width(netMain), darknet.network_height(netMain))) print("Starting the YOLO loop...") # Create an image we reuse for each detect darknet_image = darknet.make_image(darknet.network_width(netMain), darknet.network_height(netMain), 3) while True: prev_time = time.time() ret, frame_read = cap.read() frame_rgb = cv2.cvtColor(frame_read, cv2.COLOR_BGR2RGB) frame_resized = cv2.resize( frame_rgb, (darknet.network_width(netMain), darknet.network_height(netMain)), interpolation=cv2.INTER_LINEAR) darknet.copy_image_from_bytes(darknet_image, frame_resized.tobytes()) detections = darknet.detect_image(netMain, metaMain, darknet_image, thresh=0.85) data = cvDrawBoxes(detections, frame_resized) image = data[0] detection_locations = data[1] image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) if detection_locations: # This is sending a list of tuples of tuples, so to access you need to first access the list with # detection_locations[0], That will give you another tuple with two entries, each a tuple for detection in detection_locations: sendIOMessage("location", detection) # if image: # sendIOMessage("image", image) print(1 / (time.time() - prev_time)) # cv2.imshow('Demo', image) cv2.waitKey(3) cap.release() out.release()
import ultis import caches parser = argparse.ArgumentParser() parser.add_argument("--url", help="Live youtube url") parser.add_argument("--quality", help="quality of video", default=720) args = parser.parse_args() darknet_network, class_names, _ = darknet.load_network( "./cfg/vizyal.cfg", "./cfg/vizyal.data", "./models/vizyal.weights", batch_size=1 ) caches.darknet_width = darknet.network_width(darknet_network) caches.darknet_height = darknet.network_height(darknet_network) if __name__ == '__main__': ydl_opts = {} ydl = youtube_dl.YoutubeDL(ydl_opts) info_dict = ydl.extract_info(args.url, download=False) formats = info_dict.get('formats', None) frame_id = 0 for f in formats: if (f.get('height', None) == int(args.quality)): src_video = f.get('url', None) cap = cv2.VideoCapture(src_video) caches.frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) caches.frame_heights = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
assert d.ch_to_image <= d.exposures.__len__( ), "please define channels to image <= number of exposures as channels to image." assert d.ch_to_save <= d.ch_to_image, "please define channels to save as <= channels to image." d.dkrepo = git.Repo("../../darknet3AB/darknet").head.object.hexsha d.algorithm_name = "YOLOv2 Darknet3 " d.config_path = "../../darknet3AB/darknet/cfg/yolov2_dk3AB-classes-1-flip.cfg" d.meta_path = "../../cell_datasets/c127_dapi_class/2018/obj_c127_dapi_class30.data" d.weight_path = "../../models/darknet/c127_dapi_class30/yolov2_dk3AB-classes-1-no-flip_final.weights" netMain = dk.load_net_custom(d.config_path.encode("ascii"), d.weight_path.encode("ascii"), 0, 1) metaMain = dk.load_meta(d.meta_path.encode("ascii")) d.output_wid = dk.network_width(netMain) d.output_hei = dk.network_height(netMain) darknet_image = dk.make_image(d.output_wid, d.output_hei, 3) im = np.zeros((d.lim_max_y, d.lim_max_x, 3)).astype(np.uint8) spa_sam = d.digital_binning #Rather than binning we are sparse sampling. Needs improving. #####The main loop. This will keep going till all the stage positions have been visited. tfull = time.time() t0 = time.time() print("Running AMCA") path = "/media/nvidia/DOXIE_SD/0001/" imgvolume = tifffile.imread(path + "img_stk_x_-7363.1y_-454.8.tif") print(imgvolume.shape) while True: ## Collect frame t1 = time.time() frame_CH1 = imgvolume[10, :, :]
if __name__ == '__main__': db_dir = './database' MAX_FRAME = 15 THRESHOLD = 1.0 # load yolo network, class_names, colors = darknet.load_network( "yolo/fs_192.cfg", "yolo/obj.data", "yolo/yolo.weights") vid_path = 0 #vid_path = "videoplayback.mp4" cap = cv2.VideoCapture(vid_path) vid_width = int(cap.get(3)) vid_height = int(cap.get(4)) vid_fps = cap.get(5) width = darknet.network_width(network) height = darknet.network_height(network) darknet_image = darknet.make_image(width, height, 3) mot_tracker = Sort() # load face feature extraction model export_dir = "./tf131_model" sess = tf.Session(graph=tf.Graph()) tf.saved_model.loader.load(sess, [tf.saved_model.tag_constants.SERVING], export_dir) x = sess.graph.get_tensor_by_name('data:0') y = sess.graph.get_tensor_by_name('fc1/add_1:0') fd = lambda img: sess.run(y, feed_dict={x: img}) print('warm up face feature extraction') img = cv2.imread('warmup.bmp') img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) input_data = np.asarray(img, np.float32) fd(np.expand_dims(input_data, axis=0))
def YOLO(): global metaMain, netMain, altNames configPath = "./cfg/yolov3.cfg" weightPath = "./yolov3.weights" metaPath = "./cfg/coco.data" if not os.path.exists(configPath): raise ValueError("Invalid config path `" + os.path.abspath(configPath)+"`") if not os.path.exists(weightPath): raise ValueError("Invalid weight path `" + os.path.abspath(weightPath)+"`") if not os.path.exists(metaPath): raise ValueError("Invalid data file path `" + os.path.abspath(metaPath)+"`") if netMain is None: netMain = darknet.load_net_custom(configPath.encode( "ascii"), weightPath.encode("ascii"), 0, 1) # batch size = 1 if metaMain is None: metaMain = darknet.load_meta(metaPath.encode("ascii")) if altNames is None: try: with open(metaPath) as metaFH: metaContents = metaFH.read() import re match = re.search("names *= *(.*)$", metaContents, re.IGNORECASE | re.MULTILINE) if match: result = match.group(1) else: result = None try: if os.path.exists(result): with open(result) as namesFH: namesList = namesFH.read().strip().split("\n") altNames = [x.strip() for x in namesList] except TypeError: pass except Exception: pass #cap = cv2.VideoCapture(0) cap = cv2.VideoCapture("test.mp4") cap.set(3, 1280) cap.set(4, 720) out = cv2.VideoWriter( "output.avi", cv2.VideoWriter_fourcc(*"MJPG"), 10.0, (darknet.network_width(netMain), darknet.network_height(netMain))) print("Starting the YOLO loop...") # Create an image we reuse for each detect darknet_image = darknet.make_image(darknet.network_width(netMain), darknet.network_height(netMain),3) while True: prev_time = time.time() ret, frame_read = cap.read() frame_rgb = cv2.cvtColor(frame_read, cv2.COLOR_BGR2RGB) frame_resized = cv2.resize(frame_rgb, (darknet.network_width(netMain), darknet.network_height(netMain)), interpolation=cv2.INTER_LINEAR) darknet.copy_image_from_bytes(darknet_image,frame_resized.tobytes()) detections = darknet.detect_image(netMain, metaMain, darknet_image, thresh=0.25) image = cvDrawBoxes(detections, frame_resized) image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) print(1/(time.time()-prev_time)) cv2.imshow('Demo', image) cv2.waitKey(3) cap.release() out.release()