def detect_single_frame(full_body_cascade, frame, row, column, tiles_dict, **kwargs): gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) scaleFactor = kwargs.get('scaleFactor') minNeighbors = kwargs.get('minNeighbors') start_time = time.time() bodies = full_body_cascade.detectMultiScale(gray, scaleFactor, minNeighbors) total_time = time.time() - start_time boxes = [] confidences = [] frame_with_boxes = frame.copy() # Draw rectangle around the faces for (x, y, w, h) in bodies: if tiles_dict is not None: startX, startY, endX, endY = box_new_coords( [x, y, (x + w), (y + h)], row, column, tiles_dict) else: (startX, startY, endX, endY) = (x, y, (x + w), (y + h)) cv2.rectangle(frame_with_boxes, (startX, startY), (endX, endY), (0, 255, 0), 2) boxes.append([int(startX), int(startY), int(endX), int(endY)]) confidences.append(1) return boxes, confidences, total_time, frame_with_boxes
def detect_single_frame(hog, frame, row, column, tiles_dict, **kwargs): winStride = kwargs.get('winStride') padding = kwargs.get('padding') scale = kwargs.get('scale') gray = cv2.cvtColor(frame.copy(), cv2.COLOR_RGB2GRAY) start_time = time.time() detections, weights = hog.detectMultiScale(gray, winStride=winStride, padding=padding, scale=scale) total_time = time.time() - start_time boxes = [] confidences = [] frame_with_boxes = frame.copy() detections = np.array([[x, y, x + w, y + h] for (x, y, w, h) in detections]) #detections = non_max_suppression(detections, probs=None, overlapThresh=0.65) #Non-Max Supression for box in detections: if tiles_dict is not None: startX, startY, endX, endY = box_new_coords( box, row, column, tiles_dict) else: (startX, startY, endX, endY) = box cv2.rectangle(frame_with_boxes, (startX, startY), (endX, endY), (0, 255, 0), 2) boxes.append([int(startX), int(startY), int(endX), int(endY)]) confidences.append(1) return boxes, confidences, total_time, frame_with_boxes
def detect_single_frame(faster_rcnn_net, frame, row, column, tiles_dict, **kwargs): confidence = kwargs.get('confidence') threshold = kwargs.get('threshold') start_time = time.time() faster_rcnn_net.setInput(cv2.dnn.blobFromImage(frame, size=(300, 300), swapRB=True, crop=False)) detections = faster_rcnn_net.forward() total_time = time.time() - start_time boxes = [] confidences = [] frame_with_boxes = frame.copy() height = frame.shape[0] width = frame.shape[1] for detection in detections[0, 0, :, :]: class_id = int(detection[1]) if class_id == 0: # 0 is person score = float(detection[2]) if score > confidence: left = detection[3] * width top = detection[4] * height right = detection[5] * width bottom = detection[6] * height box = [int(left), int(top), int(right), int(bottom)] confidences.append(score) boxes.append(box) filtered_boxes, probs = non_max_suppression(np.array(boxes), probs=confidences, overlapThresh=0.65) final_boxes = [] final_confidences = [] for index, box in enumerate(filtered_boxes): startX, startY, endX, endY = box box = [int(startX), int(startY), int(endX), int(endY)] if tiles_dict is not None: startX, startY, endX, endY = box_new_coords(box, row, column, tiles_dict) else: (startX, startY, endX, endY) = box final_boxes.append(box) final_confidences.append(probs[index]) cv2.rectangle(frame_with_boxes, (startX, startY), (endX, endY), (0, 255, 0), 2) return final_boxes, final_confidences, total_time, frame_with_boxes
def detect_single_frame(model, frame, row, column, tiles_dict, **kwargs): (h, w) = frame.shape[:2] confidence = kwargs.get('confidence') blob = cv2.dnn.blobFromImage(cv2.resize(frame, (300, 300)), 0.007843, (300, 300), 127.5) # pass the blob through the network and obtain the detections and # predictions model.setInput(blob) start_time = time.time() detections = model.forward() total_time = time.time() - start_time boxes = [] confidences = [] frame_with_boxes = frame.copy() # loop over the detections for i in np.arange(0, detections.shape[2]): # extract the confidence (i.e., probability) associated with the # prediction _confidence = detections[0, 0, i, 2] # filter out weak detections by ensuring the `confidence` is # greater than the minimum confidence if _confidence > confidence: # extract the index of the class label from the `detections`, # then compute the (x, y)-coordinates of the bounding box for # the object idx = int(detections[0, 0, i, 1]) if MOBILE_SSD_CLASSES[idx] == 'person': box = detections[0, 0, i, 3:7] * np.array([w, h, w, h]) if tiles_dict is not None: startX, startY, endX, endY = box_new_coords( box.astype("int"), row, column, tiles_dict) else: (startX, startY, endX, endY) = box.astype("int") boxes.append([int(startX), int(startY), int(endX), int(endY)]) confidences.append(float(_confidence)) label = "{:.2f}%".format(_confidence * 100) cv2.rectangle(frame_with_boxes, (startX, startY), (endX, endY), (0, 255, 0), 2) y = startY - 15 if startY - 15 > 15 else startY + 15 cv2.putText(frame_with_boxes, label, (startX, y), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2) return boxes, confidences, total_time, frame_with_boxes
def detect_single_frame(full_body_cascade, frame, row, column, tiles_dict, **kwargs): gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) scaleFactor = kwargs.get('scaleFactor') minNeighbors = kwargs.get('minNeighbors') start_time = time.time() detections = full_body_cascade.detectMultiScale3( gray, scaleFactor, minNeighbors, minSize=(30, 30), flags=cv2.CASCADE_SCALE_IMAGE, outputRejectLevels=True) bodies = detections[0] probs = detections[2] probs = [x[0] for x in probs] try: bodies, probs = non_max_suppression(bodies, probs=probs, overlapThresh=0.65) except: print(bodies) print(probs) raise ValueError total_time = time.time() - start_time boxes = [] confidences = [] frame_with_boxes = frame.copy() # Draw rectangle around the faces for idx, (x, y, w, h) in enumerate(bodies): if tiles_dict is not None: startX, startY, endX, endY = box_new_coords( [x, y, (x + w), (y + h)], row, column, tiles_dict) else: (startX, startY, endX, endY) = (x, y, (x + w), (y + h)) cv2.rectangle(frame_with_boxes, (startX, startY), (endX, endY), (0, 255, 0), 2) boxes.append([int(startX), int(startY), int(endX), int(endY)]) confidences.append(probs[idx]) return boxes, confidences, total_time, frame_with_boxes
def detect_single_frame(yolo_net, frame, row, column, tiles_dict, **kwargs): confidence = kwargs.get('confidence') threshold = kwargs.get('threshold') start_time = time.time() detections = darknet.detect(yolo_net, meta, frame, nms=threshold) total_time = time.time() - start_time boxes = [] confidences = [] frame_with_boxes = frame.copy() for detection in detections: _class = detection[0].decode("utf-8") if _class == 'person': _confidence = detection[1] if _confidence > confidence: confidences.append(_confidence) (centerX, centerY, width, height) = detection[2] # (startX, startY, endX, endY) = detection[2] # use the center (x, y)-coordinates to derive the top # and and left corner of the bounding box x = int(centerX - (width / 2)) y = int(centerY - (height / 2)) box = [int(x), int(y), int(x+width), int(y+height)] boxes.append(box) if tiles_dict is not None: startX, startY, endX, endY = box_new_coords(box, row, column, tiles_dict) else: (startX, startY, endX, endY) = box cv2.rectangle(frame_with_boxes, (startX, startY), (endX, endY), (0, 255, 0), 2) return boxes, confidences, total_time, frame_with_boxes
def detect_single_frame(yolo_net, frame, row, column, tiles_dict, **kwargs): (H, W) = frame.shape[:2] confidence = kwargs.get('confidence') threshold = kwargs.get('threshold') # construct a blob from the input frame and then perform a forward # pass of the YOLO object detector, giving us our bounding boxes # and associated probabilities blob = cv2.dnn.blobFromImage(frame, 1 / 255.0, (416, 416), swapRB=True, crop=False) yolo_net.setInput(blob) start_time = time.time() layerOutputs = yolo_net.forward(ln) total_time = time.time() - start_time # initialize our lists of detected bounding boxes, confidences, # and class IDs, respectively boxes = [] confidences = [] classIDs = [] final_boxes = [] final_confidences = [] frame_with_boxes = frame.copy() # loop over each of the layer outputs for output in layerOutputs: # loop over each of the detections for detection in output: # extract the class ID and confidence (i.e., probability) # of the current object detection scores = detection[5:] classID = np.argmax(scores) _confidence = scores[classID] # filter out weak predictions by ensuring the detected # probability is greater than the minimum probability if _confidence > confidence: if YOLO_LABELS[int(classID)] == 'person': # scale the bounding box coordinates back relative to # the size of the image, keeping in mind that YOLO # actually returns the center (x, y)-coordinates of # the bounding box followed by the boxes' width and # height box = detection[0:4] * np.array([W, H, W, H]) (centerX, centerY, width, height) = box.astype("int") # use the center (x, y)-coordinates to derive the top # and and left corner of the bounding box x = int(centerX - (width / 2)) y = int(centerY - (height / 2)) # update our list of bounding box coordinates, # confidences, and class IDs boxes.append([x, y, int(width), int(height)]) confidences.append(float(_confidence)) classIDs.append(classID) # apply non-maxima suppression to suppress weak, overlapping bounding boxes idxs = cv2.dnn.NMSBoxes(boxes, confidences, confidence, threshold) # ensure at least one detection exists if len(idxs) > 0: # loop over the indexes we are keeping for i in idxs.flatten(): # extract the bounding box coordinates (x, y) = (boxes[i][0], boxes[i][1]) (w, h) = (boxes[i][2], boxes[i][3]) box = [x, y, (x + w), (y + h)] confidence_c = confidences[i] if tiles_dict is not None: startX, startY, endX, endY = box_new_coords(box, row, column, tiles_dict) else: (startX, startY, endX, endY) = box final_boxes.append([int(startX), int(startY), int(endX), int(endY)]) final_confidences.append(float(confidence_c)) # draw a bounding box rectangle and label on the frame cv2.rectangle(frame_with_boxes, (x, y), (x + w, y + h), (0, 255, 0), 2) text = "{}: {:.4f}".format(YOLO_LABELS[classIDs[i]], confidences[i]) cv2.putText(frame_with_boxes, text, (x, y - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2) return final_boxes, final_confidences, total_time, frame_with_boxes