def YOLO(): global metaMain, netMain, altNames configPath = "/home/marco/dev/alexeyab/darknet/cfg/obj.cfg" weightPath = "/home/marco/dev/alexeyab/darknet/backup/fourthTry/obj_130000.weights" metaPath = "/home/marco/dev/alexeyab/darknet/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(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) cap2 = cv2.VideoCapture(1) #cap = cv2.VideoCapture("/home/marco/Schreibtisch/testvideos/test_logitech1.avi") #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...") width1 = int(cap.get(3)) # float height1 = int(cap.get(4)) # float width2 = int(cap2.get(3)) # float height2 = int(cap2.get(4)) # float # 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() ret2, frame_read2 = cap2.read() frame_rgb = cv2.cvtColor(frame_read, cv2.COLOR_BGR2RGB) frame_rgb2 = cv2.cvtColor(frame_read2, cv2.COLOR_BGR2RGB) #x_start = 9 #x_end = 440 #y_start = 0 #y_end = width frame_rgb = imutils.rotate(frame_rgb, 90) frame_rgb2 = imutils.rotate(frame_rgb2, 90) #frame_rgb = frame_rgb[x_start:x_end, y_start:y_end] #frame_rgb = cv2.resize(frame_rgb, (width, height)) #frame = imutils.rotate(frame, 90) #frame2 = imutils.rotate(frame2, 90) finalframe = np.concatenate((frame_rgb, frame_rgb2), axis=1) frame_resized = cv2.resize( finalframe, (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.05) position = (-1, -1) if not (len(detections) is 0): position = (int(detections[0][2][0]), int(detections[0][2][1])) pts.appendleft(position) # loop over the set of tracked points for i in xrange(1, len(pts)): # if either of the tracked points are None, ignore # them if pts[i - 1] is None or pts[i] is None: continue # otherwise, compute the thickness of the line and # draw the connecting lines thickness = int(np.sqrt(200 / float(i + 1)) * 2) cv2.line(frame_resized, pts[i - 1], pts[i], (0, 255, 0), thickness) actualPointX = position[0] actualPointY = position[1] print("1|" + str(time.time()) + "|" + str(actualPointX) + "|" + str(actualPointY)) print("2|" + str(time.time()) + "|-1|-1") image = cvDrawBall(detections, frame_resized) image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) cv2.imshow('Demo', image) cv2.waitKey(3) cap.release() out.release()
def gen(camera): #start mqttclient client = mqtt.Client() client.on_connect = on_connect mqttport = 1883 client.connect("localhost", mqttport, 60) client.loop_start() pts = deque(maxlen=bufferSize) global metaMain, netMain, altNames configPath = "/home/marco/dev/alexeyab/darknet/cfg/obj.cfg" weightPath = "/home/marco/dev/alexeyab/darknet/backup/fourthTry/obj_130000.weights" metaPath = "/home/marco/dev/alexeyab/darknet/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(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) while True: prev_time = time.time() ret, frame_read = camera.frame.read() frame_rgb = cv2.cvtColor(frame_read, cv2.COLOR_BGR2RGB) # cut out values for the fisheye lense, dirty hack during COM x_start = 9 x_end = 440 y_start = 0 y_end = camera.width frame_rgb = frame_rgb[x_start:x_end, y_start:y_end] frame_rgb = cv2.resize(frame_rgb, (camera.width, camera.height)) 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.05) position = (-1,-1) if not (len(detections) is 0): idOfDetection = getIDHighestDetection(detections) position = (int(detections[idOfDetection][2][0]), int(detections[idOfDetection][2][1])) pts.appendleft(position) #else: # pts.append(None) # loop over the set of tracked points for i in xrange(1, len(pts)): # if either of the tracked points are None, ignore # them if pts[i - 1] is None or pts[i] is None: continue # otherwise, compute the thickness of the line and # draw the connecting lines thickness = int(np.sqrt(200 / float(i + 1)) * 2) cv2.line(frame_resized, pts[i - 1], pts[i], (0, 255, 0), thickness) if(position[0]==-1): relPointX = position[0] relPointY = position[1] else: relPointX = float(position[0])/frame_resized.shape[1] relPointY = float(position[1])/frame_resized.shape[0] client.publish("ball/position/rel", str(relPointX) + "," + str(relPointY)) image = frame_resized if not (len(detections) is 0): image = cvDrawBall(detections[idOfDetection], frame_resized) image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) #image = cv2.resize(image, (1145,680)) image = cv2.resize(image, (800,525)) if args["record"] is not 'empty': out.write(image) cv2.imshow('Demo', image) cv2.moveWindow("Demo", 1025,490); ret, jpeg = cv2.imencode('.jpg', image) frame = jpeg.tobytes() yield (b'--frame\r\n' b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n\r\n') cap.release() out.release()