Spaces:
Runtime error
Runtime error
Commit ·
a8274b9
1
Parent(s): 338e517
video function
Browse filesFiles changed (2)
- app.py+83-7
- detector/utils.py+2-48
2 unmodified lines3456789101112131415161718192 unmodified linesfrom detector.utils import detect_plates, detect_charsdef predict(img):plates = detect_plates(img)if len(plates) > 0:for plate in plates:p1, p2, crop = plateif len(crop) > 0:cv2.rectangle(img, p1, p2, (0, 0, 255), 2)text, crop = detect_chars(crop)cv2.putText(img, text, p1, cv2.FONT_HERSHEY_SIMPLEX, 4, (0, 255, 0), 5)return imgiface = gr.Interface(fn=predict, inputs="image", outputs="image")iface.launch()2 unmodified lines345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394952 unmodified linesfrom detector.utils import detect_plates, detect_charsdef fn_image(foto):plates = detect_plates(foto)plates_text = []if len(plates) > 0:for plate in plates:p1, p2, crop = plateif len(crop) > 0:cv2.rectangle(foto, p1, p2, (0, 0, 255), 2)text, crop = detect_chars(crop)cv2.putText(foto, text, p1, cv2.FONT_HERSHEY_SIMPLEX, 4, (0, 255, 0), 5)plates_text.append(text)return foto, plates_textdef fn_video(video, initial_time, duration):plates_text = []cap = cv2.VideoCapture(video)fps = cap.get(cv2.CAP_PROP_FPS)image_size = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)), int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))final_video = cv2.VideoWriter('output.mp4', cv2.VideoWriter_fourcc(*'VP90'), fps, image_size)num_frames = 0min_frame = int(initial_time * fps)max_frame = int((initial_time + duration) * fps)while cap.isOpened():try:ret, frame = cap.read()if not ret:breakexcept Exception as e:print(e)continueif num_frames < min_frame:num_frames += 1continueplates = detect_plates(frame)for plate in plates:p1, p2, crop = plateif len(crop) > 0:cv2.rectangle(frame, p1, p2, (0, 0, 255), 2)text, crop = detect_chars(crop)cv2.putText(frame, text, p1, cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 5)plates_text.append(text)final_video.write(frame)num_frames += 1if num_frames == max_frame:breakcap.release()final_video.release()return 'output.mp4', plates_textimage_interface = gr.Interface(fn=fn_image,inputs="image",outputs=["image", "text"],title="Buscar números de placa en una imagen",allow_flagging=False,allow_screenshot=False,)video_interface = gr.Interface(fn=fn_video,inputs=[gr.Video(type="file", label="Video"),gr.Slider(0, 600, value=0, label="Tiempo inicial en segundos", step=1),gr.Slider(0, 10, value=4, label="Duración en segundos", step=1),],outputs=["video", "text"],title="Buscar números de placa en un video",allow_flagging=False,allow_screenshot=False,)webcam_interface = gr.Interface(fn_image,inputs=[gr.Image(source='webcam', streaming=True),],outputs=gr.Image(type="file"),live=True,title="Buscar placa con la cámara",allow_flagging=False,allow_screenshot=False,)if __name__ == "__main__":gr.TabbedInterface([image_interface, video_interface],["Fotos", "Videos"],).launch()
6 unmodified lines7891011121314151617181929 unmodified lines495051525354551 unmodified line575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031046 unmodified linesmodel_plates = torch.hub.load('ultralytics/yolov5', 'custom',path=os.path.join(BASE_DIR, 'detector', 'static', 'plates.pt'))# model.conf = 0.60 # NMS confidence threshold# model.iou = 0.60 # NMS IoU threshold# model.agnostic = False # NMS class-agnostic# model.multi_label = False # NMS multiple labels per box# model.classes = None # (optional list) filter by class, i.e. = [0, 15, 16] for COCO persons, cats and dogs# model.max_det = 1 # maximum number of detections per image# model.amp = False # Automatic Mixed Precision (AMP) inferencemodel_chars = torch.hub.load('ultralytics/yolov5', 'custom',path=os.path.join(BASE_DIR, 'detector', 'static', 'chars.pt'))29 unmodified linesdef detect_chars(img):img = cv2.resize(img, (640, 480))detect = model_chars(img)records = detect.pandas().xyxy[0].to_dict(orient='records')yolo = np.squeeze(detect.render())1 unmodified lineif records:records = sorted(records, key=lambda d: d['xmin'])text = ''.join([i.get('name') for i in records])return text, imgdef save_plates(img):detect = model_plates(img)detect.crop(save=True)# def yolo_detections_to_norfair_detections(yolo_detections, track_points="centroid"):# """convert detections_as_xywh to norfair detections"""# norfair_detections = []## if track_points == "centroid":# detections_as_xywh = yolo_detections.xywh[0]# for detection_as_xywh in detections_as_xywh:# centroid = np.array(# [detection_as_xywh[0].item(), detection_as_xywh[1].item()]# )# scores = np.array([detection_as_xywh[4].item()])# norfair_detections.append(# Detection(# points=centroid,# scores=scores,# label=int(detection_as_xywh[-1].item()),# )# )# elif track_points == "bbox":# detections_as_xyxy = yolo_detections.xyxy[0]# for detection_as_xyxy in detections_as_xyxy:# bbox = np.array(# [# [detection_as_xyxy[0].item(), detection_as_xyxy[1].item()],# [detection_as_xyxy[2].item(), detection_as_xyxy[3].item()],# ]# )# scores = np.array(# [detection_as_xyxy[4].item(), detection_as_xyxy[4].item()]# )# norfair_detections.append(# Detection(# points=bbox, scores=scores, label=int(detection_as_xyxy[-1].item())# )# )## return norfair_detections6 unmodified lines78910111229 unmodified lines424344454647481 unmodified line5051525354555657586 unmodified linesmodel_plates = torch.hub.load('ultralytics/yolov5', 'custom',path=os.path.join(BASE_DIR, 'detector', 'static', 'plates.pt'))model_chars = torch.hub.load('ultralytics/yolov5', 'custom',path=os.path.join(BASE_DIR, 'detector', 'static', 'chars.pt'))29 unmodified linesdef detect_chars(img):img = cv2.resize(img, (640, 320))detect = model_chars(img)records = detect.pandas().xyxy[0].to_dict(orient='records')yolo = np.squeeze(detect.render())1 unmodified lineif records:records = sorted(records, key=lambda d: d['xmin'])text = ''.join([i.get('name') for i in records])return text, yolodef save_plates(img):detect = model_plates(img)detect.crop(save=True)
app.py+83-7
detector/utils.py+2-48