def predict_height(): if request.method == 'POST': if 'file' not in request.files: return 'Error: No file part, please try again' file = request.files['file'] if file.filename == '': return 'Error: No file name, please try again' if file and allowed_file(file.filename): K.clear_session() img, result_df = object_detector([file.filename], [file]) if len(img) == 0: return 'Error: Image could not be used.' # format image for return in html img = img[file.filename] tag = create_tag(img) # scale and clean data scaler = load('models/scaler.joblib') try: result_df['banana_box'] = result_df[[ 'banana_box_point1', 'banana_box_point2', 'banana_box_point3', 'banana_box_point4' ]].mean(axis=1) result_df['person_box'] = result_df[[ 'person_box_point1', 'person_box_point2', 'person_box_point3', 'person_box_point4' ]].mean(axis=1) X = scaler.transform(result_df.drop(columns=['filename'])) except: return ''' <!DOCTYPE html> <img src={0} class="img-responsive" width='400'> <p>Error: A prediction could not be made using this image <br> <i>Either a banana or person could not be detected.</i></p>'''.format( tag) # make final prediction with random forest model = load('models/randomforest.joblib') pred = model.predict(X)[0] ft = int(pred // 12) inch = round(pred % 12, 1) return render_template('prediction_answer.html', variable=[f'{ft} ft {inch} in', '', tag]) else: return redirect(request.url) return render_template('prediction.html', Title='Height Prediction with Object Detection')
def process(args): objects_detected = dict() """ #tracker_types = ['BOOSTING', 'MIL','KCF', 'TLD', 'MEDIANFLOW', 'GOTURN'] #tracker_type = tracker_types[2] #tracker = None if tracker_type == 'BOOSTING': tracker = cv.TrackerBoosting_create() if tracker_type == 'MIL': tracker = cv.TrackerMIL_create() if tracker_type == 'KCF': tracker = cv.TrackerKCF_create() if tracker_type == 'TLD': tracker = cv.TrackerTLD_create() if tracker_type == 'MEDIANFLOW': tracker = cv.TrackerMedianFlow_create() if tracker_type == 'GOTURN': tracker = cv.TrackerGOTURN_create() """ predictor = object_detector(args.model, args.config) stream = cv.VideoCapture(args.input if args.input else 0) window_name = "Tracking in progress" cv.namedWindow(window_name, cv.WINDOW_NORMAL) cv.setWindowProperty(window_name, cv.WND_PROP_AUTOSIZE, cv.WINDOW_AUTOSIZE) cv.moveWindow(window_name,10,10) if args.output: _, test_frame = stream.read() height = test_frame.shape[0] width = test_frame.shape[1] fourcc = cv.VideoWriter_fourcc(*'XVID') #out = cv.VideoWriter(args.output,fourcc, 20.0, (640,480)) out = cv.VideoWriter(args.output,fourcc, 20.0, (width,height)) failTolerance = 0 if args.classes: with open(args.classes, 'rt') as f: classes = f.read().rstrip('\n').split('\n') else: classes = list(np.arange(0,100)) stream, objects_detected, objects_list, trackers_dict = intermediate_detections(stream, predictor, args.thr, classes) while stream.isOpened(): grabbed, frame = stream.read() if not grabbed: break timer = cv.getTickCount() """ #Did not use OpenCV's multitracker because of the restrivtive nature of its Python counterpart. #If one tracker in the multitracker fails, there's no way to find out which tracker failed. #There's no easy way to delete individual trackers in the multitracker object. #Even when multitracker fails, bboxes will have old values, but 'ok' will be false #if len(objects_list) > 0: #ok, bboxes = multi_tracker.update(frame) #bboxes = multi_tracker.getObjects() #ok = multi_tracker.empty() """ print('Tracking - ',objects_list) if len(objects_detected) > 0: del_items = [] for obj,tracker in trackers_dict.items(): ok, bbox = tracker.update(frame) if ok: objects_detected[obj][0] = bbox else: print('Failed to track ', obj) del_items.append(obj) for item in del_items: trackers_dict.pop(item) objects_detected.pop(item) fps = cv.getTickFrequency() / (cv.getTickCount() - timer) if len(objects_detected) > 0: drawPred(frame, objects_detected) # Display FPS on frame cv.putText(frame, "FPS : " + str(int(fps)), (100,50), cv.FONT_HERSHEY_SIMPLEX, 0.75, (50,170,50), 2) else: cv.putText(frame, 'Tracking Failure. Trying to detect more objects', (50,80), cv.FONT_HERSHEY_SIMPLEX, 0.75,(0,0,255),2) stream, objects_detected, objects_list, trackers_dict = intermediate_detections(stream, predictor, args.thr, classes) # Display result #If resolution is too big, resize the video if frame.shape[1] > 1240: cv.imshow(window_name, cv.resize(frame, (1240, 960))) else: cv.imshow(window_name, frame) #Write to output file if args.output: out.write(frame) k = cv.waitKey(1) & 0xff #Force detect new objects if 'q' is pressed if k == ord('q'): print('Refreshing. Detecting New objects') cv.putText(frame, 'Refreshing. Detecting New objects', (100,80), cv.FONT_HERSHEY_SIMPLEX, 0.75,(0,0,255),2) stream, objects_detected, objects_list, trackers_dict = intermediate_detections(stream, predictor, args.thr, classes) # Exit if ESC pressed if k == 27 : break stream.release() if args.output: out.release() cv.destroyAllWindows()
def process(args): objects_detected = dict() tracker_types = ['BOOSTING', 'MIL', 'KCF', 'TLD', 'MEDIANFLOW', 'GOTURN'] tracker_type = tracker_types[2] tracker = None """ if tracker_type == 'BOOSTING': tracker = cv.TrackerBoosting_create() if tracker_type == 'MIL': tracker = cv.TrackerMIL_create() if tracker_type == 'KCF': tracker = cv.TrackerKCF_create() if tracker_type == 'TLD': tracker = cv.TrackerTLD_create() if tracker_type == 'MEDIANFLOW': tracker = cv.TrackerMedianFlow_create() if tracker_type == 'GOTURN': tracker = cv.TrackerGOTURN_create() """ predictor = object_detector(args.model, args.config) multi_tracker = cv.MultiTracker_create() stream = cv.VideoCapture(args.input if args.input else 0) window_name = "Tracking in progress" cv.namedWindow(window_name, cv.WINDOW_NORMAL) cv.setWindowProperty(window_name, cv.WND_PROP_AUTOSIZE, cv.WINDOW_AUTOSIZE) cv.moveWindow(window_name, 10, 10) if args.output: _, test_frame = stream.read() height = test_frame.shape[0] width = test_frame.shape[1] fourcc = cv.VideoWriter_fourcc(*'XVID') out = cv.VideoWriter(args.output, fourcc, 20.0, (width, height)) failTolerance = 0 if args.classes: with open(args.classes, 'rt') as f: classes = f.read().rstrip('\n').split('\n') else: classes = list(np.arange(0, 100)) stream, objects_detected, objects_list, multi_tracker = intermediate_detections( stream, predictor, multi_tracker, tracker, args.thr, classes) while stream.isOpened(): grabbed, frame = stream.read() if not grabbed: break timer = cv.getTickCount() if len(objects_list) > 0: ok, bboxes = multi_tracker.update(frame) fps = cv.getTickFrequency() / (cv.getTickCount() - timer) print(bboxes, ' --- ', ok) if ok and len(bboxes) > 0: drawPred(frame, bboxes, objects_detected) # Display FPS on frame cv.putText(frame, "FPS : " + str(int(fps)), (100, 50), cv.FONT_HERSHEY_SIMPLEX, 0.75, (50, 170, 50), 2) else: cv.putText(frame, 'Tracking Failure. Trying to detect more objects', (50, 80), cv.FONT_HERSHEY_SIMPLEX, 0.75, (0, 0, 255), 2) stream, objects_detected, objects_list, multi_tracker = intermediate_detections( stream, predictor, multi_tracker, tracker, args.thr, classes) # Display result #If resolution is too big, resize the video if frame.shape[1] > 1240: cv.imshow(window_name, cv.resize(frame, (1240, 960))) else: cv.imshow(window_name, frame) #Write to output file if args.output: out.write(frame) k = cv.waitKey(1) & 0xff #Force detect new objects if 'q' is pressed if k == ord('q'): print('Refreshing. Detecting New objects') cv.putText(frame, 'Refreshing. Detecting New objects', (100, 80), cv.FONT_HERSHEY_SIMPLEX, 0.75, (0, 0, 255), 2) stream, objects_detected, objects_list, multi_tracker = intermediate_detections( stream, predictor, multi_tracker, tracker, args.thr, classes) # Exit if ESC pressed if k == 27: break stream.release() if args.output: out.release() cv.destroyAllWindows()
def process(args): objects_detected = dict() cv.setUseOptimized(onoff=True) tracker = cv.TrackerKCF_create() predictor = object_detector(args.model, args.config) stream = cv.VideoCapture(args.input if args.input else 0) window_name = "Tracking in progress" cv.namedWindow(window_name, cv.WINDOW_NORMAL) cv.setWindowProperty(window_name, cv.WND_PROP_AUTOSIZE, cv.WINDOW_AUTOSIZE) cv.moveWindow(window_name, 10, 10) if args.output: _, test_frame = stream.read() height = test_frame.shape[0] width = test_frame.shape[1] fourcc = cv.VideoWriter_fourcc(*'XVID') out = cv.VideoWriter(args.output, fourcc, 20.0, (width, height)) if args.classes: with open(args.classes, 'rt') as f: classes = f.read().rstrip('\n').split('\n') else: classes = list(np.arange(0, 100)) stream, objects_detected, objects_list, trackers_dict = intermediate_detections( stream, predictor, args.thr, classes) while stream.isOpened(): grabbed, frame = stream.read() if not grabbed: break timer = cv.getTickCount() if len(objects_detected) > 0: del_items = [] for objectt, tracker in trackers_dict.items(): positive, bbox = tracker.update(frame) if positive: objects_detected[objectt][0] = bbox else: print('Failed to track ', objectt) del_items.append(objectt) for item in del_items: trackers_dict.pop(item) objects_detected.pop(item) fps = cv.getTickFrequency() / (cv.getTickCount() - timer) if len(objects_detected) > 0: drawPred(frame, objects_detected) cv.putText(frame, "FPS : " + str(int(fps)), (100, 50), cv.FONT_HERSHEY_SIMPLEX, 1.35, (50, 170, 50), 1) else: cv.putText(frame, 'Tracking Failure. Trying to detect more objects', (50, 80), cv.FONT_HERSHEY_SIMPLEX, 0.75, (0, 0, 255), 2) stream, objects_detected, objects_list, trackers_dict = intermediate_detections( stream, predictor, args.thr, classes) cv.imshow(window_name, frame) if args.output: out.write(frame) k = cv.waitKey(1) & 0xff #Refresh detection by pressing 'q' if k == ord('q'): print('Refreshing. Detecting New objects') cv.putText(frame, 'Refreshing. Detecting New objects', (100, 80), cv.FONT_HERSHEY_SIMPLEX, 0.75, (0, 0, 255), 2) stream, objects_detected, objects_list, trackers_dict = intermediate_detections( stream, predictor, args.thr, classes) #Save results and end program if 'e' pressed. if k == ord('e'): break stream.release() if args.output: out.release() cv.destroyAllWindows()