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")
def imageDetection(image, network, class_names, class_colors, thresh): # Modified from darknet_images.py width = darknet.network_width(network) height = darknet.network_height(network) darknet_image = darknet.make_image(width, height, 3) image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) image_resized = cv2.resize(image_rgb, (width, height), interpolation=cv2.INTER_LINEAR) darknet.copy_image_from_bytes(darknet_image, image_resized.tobytes()) detections = darknet.detect_image(network, class_names, darknet_image, thresh=thresh) darknet.free_image(darknet_image) image = darknet.draw_boxes(detections, image_resized, class_colors) return cv2.cvtColor(image, cv2.COLOR_BGR2RGB), detections
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")
def __exit__(self, type, value, traceback): darknet.free_image(self._img) self._img = None
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) cv2.imshow('frame', image) darknet.free_image( frame) ## darknet에서 쓰는 c언어 동적할당 해제해주는 함수(써주어야 메모리가 버틴다.) if cv2.waitKey(1) & 0xFF == ord('q'): break cap.release()
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) cv2.imshow('frame', image) if cv2.waitKey(1) & 0xFF == ord('q'): break # free_image=C++ image processing library # multi-thread 32 bit support darknet.free_image(frame) cap.release()
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")