def detect_image(self, image): if self.is_fixed_size: assert self.model_image_size[ 0] % 32 == 0, 'Multiples of 32 required' assert self.model_image_size[ 1] % 32 == 0, 'Multiples of 32 required' boxed_image = letterbox_image( image, tuple(reversed(self.model_image_size))) else: new_image_size = (image.width - (image.width % 32), image.height - (image.height % 32)) boxed_image = letterbox_image(image, new_image_size) image_data = np.array(boxed_image, dtype='float32') # print(image_data.shape) image_data /= 255. image_data = np.expand_dims(image_data, 0) # Add batch dimension. out_boxes, out_scores, out_classes = self.sess.run( [self.boxes, self.scores, self.classes], feed_dict={ self.yolo_model.input: image_data, self.input_image_shape: [image.size[1], image.size[0]], K.learning_phase(): 0 }) return_boxes = [] return_scores = [] return_class_names = [] for i, c in reversed(list(enumerate(out_classes))): predicted_class = self.class_names[c] # if ((predicted_class != 'car') or (predicted_class != 'person') or (predicted_class != 'traffic light'))==True: # Modify to detect other classes. # continue box = out_boxes[i] score = out_scores[i] # print(score) if score < 0.3: # Modify to detect other classes. continue x = int(box[1]) y = int(box[0]) w = int(box[3] - box[1]) h = int(box[2] - box[0]) if x < 0: w = w + x x = 0 if y < 0: h = h + y y = 0 return_boxes.append([x, y, w, h]) return_scores.append(score) return_class_names.append(predicted_class) return return_boxes, return_scores, return_class_names
def detect_image(self, image, model_image_size=(608, 608)): start = timer() boxed_image = letterbox_image(image, tuple(reversed(model_image_size))) image_data = np.array(boxed_image, dtype='float32') print(image_data.shape) image_data /= 255. image_data = np.expand_dims(image_data, 0) # Add batch dimension. out_boxes, out_scores, out_classes = self.sess.run( [self.boxes, self.scores, self.classes], feed_dict={ self.yolo4_model.input: image_data, self.input_image_shape: [image.size[1], image.size[0]], K.learning_phase(): 0 }) print('Found {} boxes for {}'.format(len(out_boxes), 'img')) font = ImageFont.truetype(font='font/FiraMono-Medium.otf', size=np.floor(3e-2 * image.size[1] + 0.5).astype('int32')) thickness = (image.size[0] + image.size[1]) // 300 for i, c in reversed(list(enumerate(out_classes))): predicted_class = self.class_names[c] box = out_boxes[i] score = out_scores[i] label = '{} {:.2f}'.format(predicted_class, score) draw = ImageDraw.Draw(image) label_size = draw.textsize(label, font) top, left, bottom, right = box top = max(0, np.floor(top + 0.5).astype('int32')) left = max(0, np.floor(left + 0.5).astype('int32')) bottom = min(image.size[1], np.floor(bottom + 0.5).astype('int32')) right = min(image.size[0], np.floor(right + 0.5).astype('int32')) print(label, (left, top), (right, bottom)) if top - label_size[1] >= 0: text_origin = np.array([left, top - label_size[1]]) else: text_origin = np.array([left, top + 1]) # My kingdom for a good redistributable image drawing library. for i in range(thickness): draw.rectangle([left + i, top + i, right - i, bottom - i], outline=self.colors[c]) draw.rectangle( [tuple(text_origin), tuple(text_origin + label_size)], fill=self.colors[c]) draw.text(text_origin, label, fill=(0, 0, 0), font=font) del draw end = timer() print(end - start) return image
def detect_image(self, image,cv2_img, model_image_size=(608, 608)): start = timer() boxed_image = letterbox_image(image, tuple(reversed(model_image_size))) image_data = np.array(boxed_image, dtype='float32') print(image_data.shape) image_data /= 255. image_data = np.expand_dims(image_data, 0) # Add batch dimension. out_boxes, out_scores, out_classes = self.sess.run( [self.boxes, self.scores, self.classes], feed_dict={ self.yolo4_model.input: image_data, self.input_image_shape: [image.size[1], image.size[0]], K.learning_phase(): 0 }) print('Found {} boxes for {}'.format(len(out_boxes), 'img')) # font = ImageFont.truetype(font='font/FiraMono-Medium.otf', # size=np.floor(3e-2 * image.size[1] + 0.5).astype('int32')) # thickness = (image.size[0] + image.size[1]) // 300 # COLORS = np.random.uniform(0, 255, size=(len(self.class_names), 3)).astype('int32') inf = timer() print('INference Time:-',inf-start) for i, c in reversed(list(enumerate(out_classes))): predicted_class = self.class_names[c] box = out_boxes[i] score = out_scores[i] label = '{} {:.2f}'.format(predicted_class, score) # draw = ImageDraw.Draw(image) # label_size = draw.textsize(label, font) label_size = cv2.getTextSize(label,cv2.FONT_HERSHEY_COMPLEX,0.5,2) print(label_size) top, left, bottom, right = box top = max(0, np.floor(top + 0.5).astype('int32')) left = max(0, np.floor(left + 0.5).astype('int32')) bottom = min(image.size[1], np.floor(bottom + 0.5).astype('int32')) right = min(image.size[0], np.floor(right + 0.5).astype('int32')) if top - label_size[1] >= 0: text_origin = np.array([left, top - label_size[1]]) else: text_origin = np.array([left, top + 1]) # My kingdom for a good redistributable image drawing library. # for i in range(thickness): color = self.colors[c] print(label, (left, top), (right, bottom),image.size[0],image.size[1]) cv2.rectangle(cv2_img, (left,top), (right,bottom),color, 2) cv2.putText(cv2_img, label, tuple(text_origin), cv2.FONT_HERSHEY_SIMPLEX, 0.75, color, 2) # draw.rectangle( # [left + i, top + i, right - i, bottom - i], # outline=self.colors[c]) # draw.rectangle( # [tuple(text_origin), tuple(text_origin + label_size)], # fill=self.colors[c]) # draw.text(text_origin, label, fill=(0, 0, 0), font=font) # cv2.imshow('Detections',draw) # del draw end = timer() print(end - start) return cv2_img
def detect_image(self, image): if self.is_fixed_size: assert self.model_image_size[ 0] % 32 == 0, 'Multiples of 32 required' assert self.model_image_size[ 1] % 32 == 0, 'Multiples of 32 required' boxed_image = letterbox_image( image, tuple(reversed(self.model_image_size))) else: new_image_size = (image.width - (image.width % 32), image.height - (image.height % 32)) boxed_image = letterbox_image(image, new_image_size) image_data = np.array(boxed_image, dtype='float32') # print(image_data.shape) image_data /= 255. image_data = np.expand_dims(image_data, 0) # Add batch dimension. out_boxes, out_scores, out_classes = self.sess.run( [self.boxes, self.scores, self.classes], feed_dict={ self.yolo_model.input: image_data, self.input_image_shape: [image.size[1], image.size[0]], K.learning_phase(): 0 }) #print(reversed(list(enumerate(out_classes)))) return_boxes = [] return_scores = [] return_class_names = [] return_plates = [] return_cuts = [] car_num = 0 person_num = 0 plate = '' cut_name = '' for i, c in reversed(list(enumerate(out_classes))): predicted_class = self.class_names[c] if predicted_class not in ('person', 'car', 'motorbike', 'bicycle', 'bus', 'traffic light'): continue elif predicted_class in ('car', 'bus'): car_num += 1 #检测车牌区域 if (out_scores[i] > 0.7): cut = image.crop( (int(out_boxes[i][1]), int(out_boxes[i][0]), int(out_boxes[i][3]), int(out_boxes[i][2]))) cut.save('cut' + str(i) + '.jpg') cut_name = 'cut' + str(i) + '.jpg' #appcode_demo.demo('cut'+str(i)+'.jpg') cut = numpy.array(cut) l = HyperLPR_plate_recognition(cut) #l = pp.SimpleRecognizePlate(cut) if l.__len__() != 0: print('l', l) #if (l[0][1] > 0.85): plate = [l[0][0], l[0][1]] # print(plate,l[0][1]) elif predicted_class == 'person': person_num += 1 elif predicted_class == 'traffic light': #检测红绿灯颜色 cut = image.crop((int(out_boxes[i][1]), int(out_boxes[i][0]), int(out_boxes[i][3]), int(out_boxes[i][2]))) cut = numpy.array(cut) color = self.get_color(cut) predicted_class = str(color) print(color) box = out_boxes[i] score = out_scores[i] x = int(box[1]) y = int(box[0]) w = int(box[3] - box[1]) h = int(box[2] - box[0]) if x < 0: w = w + x x = 0 if y < 0: h = h + y y = 0 return_boxes.append([x, y, w, h]) return_scores.append(score) return_class_names.append(predicted_class) return_plates.append(plate) return_cuts.append(cut_name) plate = '' cut_name = '' return return_boxes, return_scores, return_class_names, return_plates, car_num, person_num, return_cuts
def detect_image(self, image, model_image_size=(608, 608)): start = timer() boxed_image = letterbox_image(image, tuple(reversed(model_image_size))) image_data = np.array(boxed_image, dtype='float32') print(image_data.shape) image_data /= 255. image_data = np.expand_dims(image_data, 0) # Add batch dimension. out_boxes, out_scores, out_classes = self.sess.run( [self.boxes, self.scores, self.classes], feed_dict={ self.yolo4_model.input: image_data, self.input_image_shape: [image.size[1], image.size[0]], # K.learning_phase(): 0 }) print('Found {} boxes for {}'.format(len(out_boxes), 'img')) font = ImageFont.truetype(font='font/FiraMono-Medium.otf', size=np.floor(3e-2 * image.size[1] + 0.5).astype('int32')) thickness = (image.size[0] + image.size[1]) // 300 listPositions = {} listLaybel = {} p = {} ll = [] la = [] z = 0 for i, c in reversed(list(enumerate(out_classes))): predicted_class = self.class_names[c] box = out_boxes[i] score = out_scores[i] label = '{} {:.2f}%'.format(predicted_class, score * 100) draw = ImageDraw.Draw(image) label_size = draw.textsize(label, font) sc = '{:.2f}%'.format(score * 100) top, left, bottom, right = box top = max(0, np.floor(top + 0.5).astype('int32')) left = max(0, np.floor(left + 0.5).astype('int32')) bottom = min(image.size[1], np.floor(bottom + 0.5).astype('int32')) right = min(image.size[0], np.floor(right + 0.5).astype('int32')) print(label, (left, top), (right, bottom)) name = label rs = (predicted_class, left, top, right, bottom) p = { "left": int(left), "top": int(top), "right": int(right), "bottom": int(bottom) } # listPositions['item'+str(z)] = { # 'name': name # } z += 1 listLaybel['item' + str(z)] = name ll.append({'name': predicted_class, 'score': sc, 'position': p}) # cv2.rectangle(image, (int(left), int(top)), # (int(right), int(bottom)), (0, 255, 0), 5) if top - label_size[1] >= 0: text_origin = np.array([left, top - label_size[1]]) else: text_origin = np.array([left, top + 1]) # My kingdom for a good redistributable image drawing library. for i in range(thickness): draw.rectangle([left + i, top + i, right - i, bottom - i], outline=self.colors[c]) draw.rectangle( [tuple(text_origin), tuple(text_origin + label_size)], fill=self.colors[c]) draw.text(text_origin, label, fill=(0, 0, 0), font=font) del draw image.save('img/abc.jpg') print(Path().absolute()) end = timer() return ll
def detect_image(self, image): if self.is_fixed_size: assert self.model_image_size[ 0] % 32 == 0, 'Multiples of 32 required' assert self.model_image_size[ 1] % 32 == 0, 'Multiples of 32 required' boxed_image = letterbox_image( image, tuple(reversed(self.model_image_size))) else: new_image_size = (image.width - (image.width % 32), image.height - (image.height % 32)) boxed_image = letterbox_image(image, new_image_size) image_data = np.array(boxed_image, dtype='float32') image_data /= 255. image_data = np.expand_dims(image_data, 0) # Add batch dimension. out_boxes, out_scores, out_classes = self.sess.run( [self.boxes, self.scores, self.classes], feed_dict={ self.yolo_model.input: image_data, self.input_image_shape: [image.size[1], image.size[0]], K.learning_phase(): 0 }) return_boxs = [] return_class_name = [] person_counter = 0 for i, c in reversed(list(enumerate(out_classes))): predicted_class = self.class_names[c] if predicted_class != 'person' \ and predicted_class != 'bicycle' \ and predicted_class != 'car' \ and predicted_class != 'motorbike'\ and predicted_class != 'bus'\ and predicted_class != 'truck'\ and predicted_class != 'stop sign'\ and predicted_class != 'traffic light': # and predicted_class != ''\ # and predicted_class != '': print(predicted_class) continue # if predicted_class != args["class"]:#and predicted_class != 'car': # #print(predicted_class) # continue person_counter += 1 #if predicted_class != 'car': #continue #label = predicted_class box = out_boxes[i] #score = out_scores[i] x = int(box[1]) y = int(box[0]) w = int(box[3] - box[1]) h = int(box[2] - box[0]) if x < 0: w = w + x x = 0 if y < 0: h = h + y y = 0 return_boxs.append([x, y, w, h]) #print(return_boxs) return_class_name.append([predicted_class]) #cv2.putText(image, str(self.class_names[c]),(int(box[0]), int(box[1] -50)),0, 5e-3 * 150, (0,255,0),2) #print("Found person: ",person_counter) return return_boxs, return_class_name