Spaces:
Runtime error
Runtime error
Commit ·
c683b90
1
Parent(s): a8274b9
add sorter and improve detections
Browse filesFiles changed (2)
- app.py+65-11
- detector/utils.py+2-16
12345611 unmodified lines181920212223241 unmodified line2627282930312 unmodified lines343536373839404142434445464748495051525354555657import gradio as grimport cv2from detector.utils import detect_plates, detect_charsdef fn_image(foto):11 unmodified linesdef 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)))1 unmodified linenum_frames = 0min_frame = int(initial_time * fps)max_frame = int((initial_time + duration) * fps)while cap.isOpened():try:ret, frame = cap.read()2 unmodified linesexcept 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(12345678910111213141516171819202122232425262728293031323311 unmodified lines454647484950515253541 unmodified line565758596061622 unmodified lines6566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111import gradio as grimport numpy as npimport cv2from norfair import Detection, Tracker, Videofrom detector.utils import detect_plates, detect_chars, imcropDISTANCE_THRESHOLD_BBOX: float = 0.7DISTANCE_THRESHOLD_CENTROID: int = 30MAX_DISTANCE: int = 10000def yolo_to_norfair(yolo_detections):norfair_detections = []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_detectionsdef fn_image(foto):11 unmodified linesdef fn_video(video, initial_time, duration):tracker = Tracker(distance_function="iou_opt",distance_threshold=DISTANCE_THRESHOLD_BBOX,)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)))1 unmodified linenum_frames = 0min_frame = int(initial_time * fps)max_frame = int((initial_time + duration) * fps)plates = {}while cap.isOpened():try:ret, frame = cap.read()2 unmodified linesexcept Exception as e:print(e)continue# num_frames += 1# if num_frames % 3 != 0:# continueif num_frames < min_frame:num_frames += 1continueyolo_detections = detect_plates(frame)detections = yolo_to_norfair(yolo_detections)tracked_objects = tracker.update(detections=detections)for obj in tracked_objects:if obj.last_detection is not None:bbox = obj.last_detection.pointsbbox = int(bbox[0][0]), int(bbox[0][1]), int(bbox[1][0]), int(bbox[1][1])if obj.id not in plates.keys():crop = imcrop(frame, bbox)text = detect_chars(crop)plates[obj.id] = textcv2.rectangle(frame,(bbox[0], bbox[1]),(bbox[2], bbox[3]),(0, 255, 0),2,)cv2.putText(frame,plates[obj.id],(bbox[0], bbox[1]),cv2.FONT_HERSHEY_SIMPLEX,1,(0, 255, 0),2,)final_video.write(frame)num_frames += 1if num_frames == max_frame:breakcap.release()final_video.release()return 'output.mp4', [plates[k] for k in plates.keys()]image_interface = gr.Interface(
29 unmodified lines303132333435363738394041424344454647484950515253545556575829 unmodified linesdef detect_plates(img):detect = model_plates(img)records = detect.pandas().xyxy[0].to_dict(orient='records')plates = []if records:for plate in records:xi, yi, xf, yf = int(plate['xmin']), int(plate['ymin']), int(plate['xmax']), int(plate['ymax'])crop = imcrop(img, (xi, yi, xf, yf))plates.append(((xi, yi), (xf, yf), crop))return platesdef 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())text = ''if 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)29 unmodified lines30313233343536373839404142434429 unmodified linesdef detect_plates(img):return model_plates(img)def detect_chars(img):img = cv2.resize(img, (640, 320))detect = model_chars(img)records = detect.pandas().xyxy[0].to_dict(orient='records')text = ''if records:records = sorted(records, key=lambda d: d['xmin'])text = ''.join([i.get('name') for i in records])return text
app.py+65-11
detector/utils.py+2-16