{"action":"created","issue":{"url":"https://api.github.com/repos/AlexeyAB/darknet/issues/1566","repository_url":"https://api.github.com/repos/AlexeyAB/darknet","labels_url":"https://api.github.com/repos/AlexeyAB/darknet/issues/1566/labels{/name}","comments_url":"https://api.github.com/repos/AlexeyAB/darknet/issues/1566/comments","events_url":"https://api.github.com/repos/AlexeyAB/darknet/issues/1566/events","html_url":"https://github.com/AlexeyAB/darknet/issues/1566","id":358395949,"node_id":"MDU6SXNzdWUzNTgzOTU5NDk=","number":1566,"title":"I've changed darknet.py to work with OpenCV webcam but the results are not the same as the C version","user":{"login":"goktug97","id":20567087,"node_id":"MDQ6VXNlcjIwNTY3MDg3","avatar_url":"https://avatars1.githubusercontent.com/u/20567087?v=4","gravatar_id":"","url":"https://api.github.com/users/goktug97","html_url":"https://github.com/goktug97","followers_url":"https://api.github.com/users/goktug97/followers","following_url":"https://api.github.com/users/goktug97/following{/other_user}","gists_url":"https://api.github.com/users/goktug97/gists{/gist_id}","starred_url":"https://api.github.com/users/goktug97/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/goktug97/subscriptions","organizations_url":"https://api.github.com/users/goktug97/orgs","repos_url":"https://api.github.com/users/goktug97/repos","events_url":"https://api.github.com/users/goktug97/events{/privacy}","received_events_url":"https://api.github.com/users/goktug97/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2018-09-09T17:34:58Z","updated_at":"2018-09-09T21:43:18Z","closed_at":null,"author_association":"NONE","body":"Hello,\r\nI needed YOLO that works with python2.7 so I took the darknet.py file and modified to work with OpenCV and webcam but the problem is the output isn't as good as the darknet.exe.\r\nI've made the following changes.\r\n\r\ndetect function, I didn't copied all of them because the rest is same and I commented the free_image line.\r\n```\r\n    im, arr = array_to_image(image)\r\n    if debug:\r\n        print(\"Loaded image\")\r\n    num = c_int(0)\r\n    if debug:\r\n        print(\"Assigned num\")\r\n    pnum = pointer(num)\r\n    if debug:\r\n        print(\"Assigned pnum\")\r\n    predict_image(net, im)\r\n    if debug:\r\n        print(\"did prediction\")\r\n    # dets = get_network_boxes(\r\n    #     net, image.shape[1], image.shape[0],\r\n    #     thresh, hier_thresh,\r\n    #     None, 0, pnum, 0)  # OpenCV\r\n    dets = get_network_boxes(net, im.w, im.h,\r\n                             thresh, hier_thresh, None, 0, pnum, 0)\r\n```\r\nand my YOLO function looks like this. I deleted the weight loading part they are same.\r\n```\r\ndef YOLO(yolo_array, isReady):\r\n    global metaMain, netMain, altNames\r\n    cap = cv2.VideoCapture(0)\r\n    cap.set(3, 1280)\r\n    cap.set(4, 720)\r\n    out = cv2.VideoWriter(\r\n        \"output.avi\", cv2.VideoWriter_fourcc(*\"MJPG\"), 5.0,\r\n        (lib.network_width(netMain), lib.network_height(netMain)))\r\n    print(\"Starting the YOLO loop...\")\r\n    isReady.value = 0\r\n    while True:\r\n        ret, frame_read = cap.read()\r\n        frame_rgb = cv2.cvtColor(frame_read, cv2.COLOR_BGR2RGB)\r\n        detections = detect(netMain, metaMain, frame_rgb, thresh=0.25)\r\n        if not isReady.value:\r\n            counter = 0\r\n            yolo_array[0] = len(detections)\r\n            for detection in detections:\r\n                drone = detection[1]\r\n                x = detection[2][0]\r\n                y = detection[2][1]\r\n                w = detection[2][2]\r\n                h = detection[2][3]\r\n                unrolled = [drone, x, y, w, h]\r\n                yolo_array[1 + (counter*5):1 + (counter*5)+5] = unrolled[:]\r\n                counter += 1\r\n            isReady.value = 1\r\n        image = cvDrawBoxes(detections, frame_read)\r\n        frame_resized = cv2.resize(image,\r\n                                   (lib.network_width(netMain),\r\n                                    lib.network_height(netMain)),\r\n                                   interpolation=cv2.INTER_LINEAR)\r\n        cv2.imshow('image', frame_resized)\r\n        out.write(frame_resized)\r\n    cap.release()\r\n    out.release()\r\n```\r\nisReady and yolo_array is shared Value.\r\n\r\nThis part is just writing to shared array.\r\n```\r\n        if not isReady.value:\r\n            counter = 0\r\n            yolo_array[0] = len(detections)\r\n            for detection in detections:\r\n                drone = detection[1]\r\n                x = detection[2][0]\r\n                y = detection[2][1]\r\n                w = detection[2][2]\r\n                h = detection[2][3]\r\n                unrolled = [drone, x, y, w, h]\r\n                yolo_array[1 + (counter*5):1 + (counter*5)+5] = unrolled[:]\r\n                counter += 1\r\n            isReady.value = 1\r\n```\r\nand cvDrawBoxes is the function from\r\n[here](https://github.com/pjreddie/darknet/issues/289#issuecomment-369844329)\r\n\r\nI'm starting the YOLO with this file.\r\n```\r\nfrom multiprocessing import Value, Process, Array, Queue\r\nfrom darknetv3 import YOLO\r\nimport time\r\n\r\n\r\ndef DroneLoop(yolo_outputs):\r\n    for yolo_output in yolo_outputs:\r\n        print(yolo_output)\r\n    print(\"\\n\")\r\n\r\n\r\ndef Drone(yolo_array, isReady):\r\n    yolo_outputs = []\r\n    print('Starting the Drone...')\r\n    while(1):  # DRONE LOOP\r\n        if(isReady.value):\r\n            local_yolo_array = yolo_array[:]\r\n            isReady.value = 0\r\n            length = int(local_yolo_array[0])\r\n            for i in range(length):\r\n                yolo_outputs.append(local_yolo_array[1 + (i*5):1 + (i*5)+5])\r\n            DroneLoop(yolo_outputs)\r\n            yolo_outputs = []\r\n\r\n\r\nif __name__ == \"__main__\":\r\n    max_numbers_to_detect = 30\r\n\r\n    yolo_array = Array('d', range(max_numbers_to_detect*5+1))\r\n    isReady = Value('i', 3)\r\n     yolo = Process(target=YOLO, args=(\r\n     yolo_array, isReady, ))\r\n    drone = Process(target=Drone, args=(yolo_array, isReady,))\r\n    yolo.start()\r\n    while isReady.value == 3:\r\n        pass\r\n    drone.start()\r\n    yolo.join()\r\n    drone.join()\r\n```\r\n\r\nI converted 1280x720 feed from BGR to RGB, feed it to the detect function without resizing it. detect function converts it to image with array_to_image and the rest of the detect function is same. After taking the results, I draw boxes to the BGR image and write it to the video but the detection performance didn't look the same.\r\nPython version:\r\n![image](https://user-images.githubusercontent.com/20567087/45267057-123a1a00-b46e-11e8-9802-2051a50fbd08.png)\r\n./darknet demo version:\r\n![image](https://user-images.githubusercontent.com/20567087/45267139-90e38700-b46f-11e8-9ad2-f11507f02ae5.png)\r\n\r\nand with constant shake in camera the demo version couldn't put the rectangles into right spot.\r\nSorry for the bad picture I don't have access to the last video but it shows the situation. Am I using wrong format of the frames to feed to the YOLO.\r\n\r\n"},"comment":{"url":"https://api.github.com/repos/AlexeyAB/darknet/issues/comments/419747154","html_url":"https://github.com/AlexeyAB/darknet/issues/1566#issuecomment-419747154","issue_url":"https://api.github.com/repos/AlexeyAB/darknet/issues/1566","id":419747154,"node_id":"MDEyOklzc3VlQ29tbWVudDQxOTc0NzE1NA==","user":{"login":"AlexeyAB","id":4096485,"node_id":"MDQ6VXNlcjQwOTY0ODU=","avatar_url":"https://avatars3.githubusercontent.com/u/4096485?v=4","gravatar_id":"","url":"https://api.github.com/users/AlexeyAB","html_url":"https://github.com/AlexeyAB","followers_url":"https://api.github.com/users/AlexeyAB/followers","following_url":"https://api.github.com/users/AlexeyAB/following{/other_user}","gists_url":"https://api.github.com/users/AlexeyAB/gists{/gist_id}","starred_url":"https://api.github.com/users/AlexeyAB/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/AlexeyAB/subscriptions","organizations_url":"https://api.github.com/users/AlexeyAB/orgs","repos_url":"https://api.github.com/users/AlexeyAB/repos","events_url":"https://api.github.com/users/AlexeyAB/events{/privacy}","received_events_url":"https://api.github.com/users/AlexeyAB/received_events","type":"User","site_admin":false},"created_at":"2018-09-09T21:43:18Z","updated_at":"2018-09-09T21:43:18Z","author_association":"OWNER","body":"@goktug97 Hello,\r\n\r\n>         ret, frame_read = cap.read()\r\n>         frame_rgb = cv2.cvtColor(frame_read, cv2.COLOR_BGR2RGB)\r\n>         detections = detect(netMain, metaMain, frame_rgb, thresh=0.25)\r\n\r\n> Am I using wrong format of the frames to feed to the YOLO.\r\n\r\nI think you are using correct format of the frames.\r\n\r\nBut I recommend to resize image by using OpenCV in the Python code, it will remove CPU bottleneck, since `cv2.resize()` is faster than Darknet `resize_image()`, as it is done here: \r\nhttps://github.com/AlexeyAB/darknet/blob/57e878b4f9512cf9995ff6b5cd6e0d7dc1da9eaf/darknet.py#L227-L229\r\n\r\n----\r\n\r\nThe main difference in that, C version of Yolo averages results from 3 last frames: https://github.com/AlexeyAB/darknet/blob/57e878b4f9512cf9995ff6b5cd6e0d7dc1da9eaf/src/demo.c#L89-L93\r\nBut usually it decreases False Positives, but doesn't decrease False Negative (that is in your case). So may be there is something else.\r\nTo check it, just try to set here `#define FRAMES 3` instead of: https://github.com/AlexeyAB/darknet/blob/57e878b4f9512cf9995ff6b5cd6e0d7dc1da9eaf/src/demo.c#L18\r\nand comapre result of you Python-code and Darknet-Yolo again.\r\nWill be these results different too?\r\n\r\n\r\n"}}
{"actor":{"display_login":"AlexeyAB"}}