def detect(self, image): boxes = [] classes_id = [] confidences = [] scale = 0.00392 blob = cv2.dnn.blobFromImage(image, scalefactor=scale, size=(416, 416), mean=(0, 0), swapRB=True, crop=False) height, width = image.shape[:2] # Lấy ảnh đầu vào cho model self.model.setInput(blob) # Thực hiện tính toán forward outputs = self.model.forward(utils.get_output_layers(self.model)) # Xử lý các vùng có khả năng là biển số xe for output in outputs: for i in range(len(output)): scores = output[i][5:] class_id = np.argmax(scores) confidence = float(scores[class_id]) # Chọn vùng có độ tin cậy rằng vùng đó khả năng lớn là biển số if confidence > self.threshold: # coordinate of bounding boxes center_x = int(output[i][0] * width) center_y = int(output[i][1] * height) detected_width = int(output[i][2] * width) detected_height = int(output[i][3] * height) x_min = center_x - detected_width / 2 y_min = center_y - detected_height / 2 boxes.append( [x_min, y_min, detected_width, detected_height]) classes_id.append(class_id) confidences.append(confidence) indices = cv2.dnn.NMSBoxes(boxes, confidences, score_threshold=self.threshold, nms_threshold=0.4) coordinates = [] # Lưu giá trị của các vùng chắc chắn là biển số for i in indices: index = i[0] x_min, y_min, width, height = boxes[index] x_min = round(x_min) y_min = round(y_min) coordinates.append((x_min, y_min, width, height)) return coordinates
def detect(self, image): boxes = [] classes_id = [] confidences = [] scale = 0.00392 blob = cv2.dnn.blobFromImage(image, scalefactor=scale, size=(416, 416), mean=(0, 0), swapRB=True, crop=False) height, width = image.shape[:2] # take image to model self.model.setInput(blob) # run forward outputs = self.model.forward(utils.get_output_layers(self.model)) for output in outputs: for i in range(len(output)): scores = output[i][5:] class_id = np.argmax(scores) confidence = float(scores[class_id]) if confidence > self.threshold: # coordinate of bounding boxes center_x = int(output[i][0] * width) center_y = int(output[i][1] * height) detected_width = int(output[i][2] * width) detected_height = int(output[i][3] * height) x_min = center_x - detected_width / 2 y_min = center_y - detected_height / 2 boxes.append( [x_min, y_min, detected_width, detected_height]) classes_id.append(class_id) confidences.append(confidence) indices = cv2.dnn.NMSBoxes(boxes, confidences, score_threshold=self.threshold, nms_threshold=0.4) coordinates = [] for i in indices: index = i[0] x_min, y_min, width, height = boxes[index] x_min = round(x_min) y_min = round(y_min) coordinates.append((x_min, y_min, width, height)) return coordinates
def detect(self, img, bname, pixel_threshold=100000): output_dir = "output/tmp" boxes = [] classes_id = [] confidences = [] scale = 0.00392 blob = cv2.dnn.blobFromImage(img, scalefactor=scale, size=(416, 416), mean=(0, 0, 0), swapRB=True, crop=False) height, width = img.shape[:2] # take image to model self.model.setInput(blob) # run forward outputs = self.model.forward(utils.get_output_layers(self.model)) for out in outputs: for detection in out: scores = detection[5:] class_id = np.argmax(scores) confidence = float(scores[class_id]) if confidence > self.threshold: # coordinate of bounding boxes center_x = int(detection[0] * width) center_y = int(detection[1] * height) w = int(detection[2] * width) h = int(detection[3] * height) #cv2.circle(img,(center_x,center_y),10,(0,255,0),2) #rectangle co-ordinaters x = int(center_x - w / 2) y = int(center_y - h / 2) #cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),2) boxes.append([x, y, w, h]) classes_id.append(class_id) confidences.append(confidence) indices = cv2.dnn.NMSBoxes(boxes, confidences, score_threshold=self.threshold, nms_threshold=0.3) R = [] for i in range(len(boxes)): if i in indices: x, y, w, h = boxes[i] label = str(self.labels[classes_id[i]]) R.append((label, confidences[i], (x, y, w, h))) R = sorted(R, key=lambda x: -x[1]) R = [r for r in R if r[0] in ['car', 'bus']] cars_img = [] Lcars = [] if len(R): # Iorig = cv2.imread(img_path) Iorig = img WH = np.array(Iorig.shape[1::-1], dtype=float) for i, r in enumerate(R): # print("car size: w={}, h={}, area={}", r[2][2], r[2][3], r[2][2] * r[2][3]) _, _, ori_w, ori_h = r[2] if ori_w * ori_h >= pixel_threshold: cx, cy, w, h = (np.array(r[2]) / np.concatenate( (WH, WH))).tolist() tl = np.array([cx, cy]) br = np.array([cx + w, cy + h]) label = Label(0, tl, br) Icar = crop_region(Iorig, label) Lcars.append(label) # cv2.imwrite('{}/{}_{}car.png'.format(output_dir, bname, i), Icar) cars_img.append(Icar) # lwrite('{}/{}_cars.txt'.format(output_dir, bname), Lcars) print('\t\t{} cars found by using detect_vehicle'.format( len(cars_img))) return cars_img, Lcars