Beispiel #1
0
def gen(camera):
    """Video streaming generator function."""
    while True:
        image = camera.read()
        image = cv2.resize(image,
                           dsize=(640, 480),
                           interpolation=cv2.INTER_AREA)
        frame = darknet.nparray_to_image(image)
        r = darknet.detect_image(net,
                                 meta,
                                 frame,
                                 thresh=.5,
                                 hier_thresh=.5,
                                 nms=.45,
                                 debug=False)
        boxes = []

        for k in range(len(r)):
            width = r[k][2][2]
            height = r[k][2][3]
            center_x = r[k][2][0]
            center_y = r[k][2][1]
            bottomLeft_x = center_x - (width / 2)
            bottomLeft_y = center_y - (height / 2)
            x, y, w, h = bottomLeft_x, bottomLeft_y, width, height
            boxes.append((x, y, w, h))

        for k in range(len(boxes)):
            x, y, w, h = boxes[k]
            top = max(0, np.floor(x + 0.5).astype(int))
            left = max(0, np.floor(y + 0.5).astype(int))
            right = min(image.shape[1], np.floor(x + w + 0.5).astype(int))
            bottom = min(image.shape[0], np.floor(y + h + 0.5).astype(int))
            cv2.rectangle(image, (top, left), (right, bottom), (255, 0, 0), 2)
            cv2.line(image, (top + int(w / 2), left),
                     (top + int(w / 2), left + int(h)), (0, 255, 0), 3)
            cv2.line(image, (top, left + int(h / 2)),
                     (top + int(w), left + int(h / 2)), (0, 255, 0), 3)
            cv2.circle(image, (top + int(w / 2), left + int(h / 2)), 2,
                       tuple((0, 0, 255)), 5)

        ret, jpeg = cv2.imencode('.jpg', image)
        darknet.free_image(frame)
        # print("after get_frame")
        if jpeg is not None:
            yield (b'--frame\r\n'
                   b'Content-Type: image/jpeg\r\n\r\n' + jpeg.tobytes() +
                   b'\r\n')
        else:
            print("frame is none")
Beispiel #2
0
print(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))

#3. opencv로 darknet 실행
# 비디오 캡쳐 객체가 정상으로 open 되는 동안 반복
while (cap.isOpened()):
    # 비디오의 한 프레임씩 읽기
    # 제대로 읽으면 ret=True, 읽은 프레임은 image 변수
    ret, image = cap.read()
    # resize하기 위해서는 픽셀 사이의 값을 결정. INTER_AREA=사이즈를 줄이기 위한 보간법
    image = cv2.resize(image, dsize=(640, 480), interpolation=cv2.INTER_AREA)
    print(image.shape)

    if not ret:
        break

    frame = darknet.nparray_to_image(image)
    r = darknet.detect_image(net, meta, frame)

    boxes = []

    for k in range(len(r)):
        width = r[k][2][2]
        height = r[k][2][3]
        center_x = r[k][2][0]
        center_y = r[k][2][1]
        bottomLeft_x = center_x - (width / 2)
        bottomLeft_y = center_y - (height / 2)
        x, y, w, h = bottomLeft_x, bottomLeft_y, width, height
        boxes.append((x, y, w, h))

    for k in range(len(boxes)):
Beispiel #3
0
def gen(camera):
    # """Video streaming generator function."""
    i = 0
    while True:
        i += 1
        image = camera.read()
        image = cv2.resize(image, dsize=(640, 480),
                           interpolation=cv2.INTER_AREA)
        print(i)
        # if not ret:
        # break
        frame = darknet.nparray_to_image(image)
        r = darknet.detect_image(net, meta, frame, thresh=.5, hier_thresh=.5, nms=.45, debug=False)
        print(f"r:\n{r}")
        # [(b'normal', 0.9838562607765198, (337.76190185546875, 226.85903930664062, 41.72311782836914, 109.13109588623047)), 
        # (b'normal', 0.907978355884552, (302.71875, 253.96533203125, 41.06242752075195, 113.02967834472656)), 
        # (b'normal', 0.8925231695175171, (377.8631286621094, 233.21629333496094, 32.55954360961914, 110.92288970947266))]
        boxes = []

        for k in range(len(r)):
            width = r[k][2][2]
            height = r[k][2][3]
            center_x = r[k][2][0]
            center_y = r[k][2][1]
            bottomLeft_x = center_x - (width / 2)
            bottomLeft_y = center_y - (height / 2)
            x, y, w, h = bottomLeft_x, bottomLeft_y, width, height
            mytexts = r[k][0]
            mythresh = r[k][1]
            boxes.append((x, y, w, h, mytexts, mythresh))
        print("next")

        for k in range(len(boxes)):
            x, y, w, h, texts, threshs = boxes[k]
            top = max(0, np.floor(x + 0.5).astype(int))
            left = max(0, np.floor(y + 0.5).astype(int))
            right = min(image.shape[1], np.floor(x + w + 0.5).astype(int))
            bottom = min(image.shape[0], np.floor(y + h + 0.5).astype(int))
            # cv2.rectangle(image, (top, left), (right, bottom), (0, 255, 0), 1)

            if texts.decode('utf-8') == 'normal':
                cv2.rectangle(image, (top, left),
                              (right, bottom), (255, 0, 0), 2)
                cv2.putText(image, texts.decode('utf-8') + '(' + str(threshs*100)
                            [:5] + '%)', (top, left-5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 0))

            elif texts.decode('utf-8') == 'fighting':
                cv2.rectangle(image, (top, left),
                              (right, bottom), (0, 0, 255), 2)
                # mark probablity
                cv2.putText(image, texts.decode('utf-8') + '(' + str(threshs*100)
                            [:5] + '%)', (top, left-5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255))


        # cv2.imshow('frame', image)
        
        ret, jpeg = cv2.imencode('.jpg', image)

        # darknet dynamic allocation
        darknet.free_image(frame)

        if jpeg is not None:
            yield (b'--frame\r\n'
                   b'Content-Type: image/jpeg\r\n\r\n' + jpeg.tobytes() + b'\r\n')
        else:
            print("frame is none")
Beispiel #4
0
def gen(camera):
    # """Video streaming generator function."""
    i = 0
    while True:
        i += 1
        image = camera.read()
        image = cv2.resize(image,
                           dsize=(640, 480),
                           interpolation=cv2.INTER_AREA)
        print(i)
        # if not ret:
        # break
        frame = darknet.nparray_to_image(image)
        r = darknet.detect_image(net,
                                 meta,
                                 frame,
                                 thresh=.5,
                                 hier_thresh=.5,
                                 nms=.45,
                                 debug=False)
        print(r)
        boxes = []

        for k in range(len(r)):
            width = r[k][2][2]
            height = r[k][2][3]
            center_x = r[k][2][0]
            center_y = r[k][2][1]
            bottomLeft_x = center_x - (width / 2)
            bottomLeft_y = center_y - (height / 2)
            x, y, w, h = bottomLeft_x, bottomLeft_y, width, height
            mytexts = r[k][0]
            mythresh = r[k][1]
            boxes.append((x, y, w, h, mytexts, mythresh))
        print(1)

        for k in range(len(boxes)):
            x, y, w, h, texts, threshs = boxes[k]
            top = max(0, np.floor(x + 0.5).astype(int))
            left = max(0, np.floor(y + 0.5).astype(int))
            right = min(image.shape[1], np.floor(x + w + 0.5).astype(int))
            bottom = min(image.shape[0], np.floor(y + h + 0.5).astype(int))
            # cv2.rectangle(image, (top, left), (right, bottom), (0, 255, 0), 1)

            if texts.decode('utf-8') == 'normal':
                cv2.rectangle(image, (top, left), (right, bottom), (255, 0, 0),
                              2)
                cv2.putText(
                    image,
                    texts.decode('utf-8') + '(' + str(threshs * 100)[:5] +
                    '%)', (top, left - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.5,
                    (255, 0, 0))

            elif texts.decode('utf-8') == 'fighting':
                cv2.rectangle(image, (top, left), (right, bottom), (0, 0, 255),
                              2)
                cv2.putText(
                    image,
                    texts.decode('utf-8') + '(' + str(threshs * 100)[:5] +
                    '%)', (top, left - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.5,
                    (0, 0, 255))

        ret, jpeg = cv2.imencode('.jpg', image)
        darknet.free_image(
            frame)  ## darknet에서 쓰는 c언어 동적할당 해제해주는 함수(써주어야 메모리가 버틴다.)

        if jpeg is not None:
            yield (b'--frame\r\n'
                   b'Content-Type: image/jpeg\r\n\r\n' + jpeg.tobytes() +
                   b'\r\n')
        else:
            print("frame is none")