print('Starting Video Analysis...') with open('ssd/label_mapping.csv') as f: mapping = { int(x.split(',')[0].strip()): x.split(',')[1].strip() for x in f.readlines() } vs = cv2.VideoCapture(video) while True: ret, frame = vs.read() if not ret: break results = detector.getBoundingBoxes(frame) if len(results) == 0: continue detections = [] for res in results: det_id = 'N/A' if jaguars_identifier.belongsToClass(res.classid): det_id = jaguars_identifier.getIdFromBoundingBox( frame, res.bounding_box) elif tigers_identifier.belongsToClass(res.classid): det_id = tigers_identifier.getIdFromBoundingBox( frame, res.bounding_box) elif elephants_identifier.belongsToClass(res.classid): det_id = elephants_identifier.getIdFromBoundingBox(
idNum = None if len(args) > 4: idNum = int(args[4]) rev_map = {} for x in id_map: if not x[1] in rev_map: rev_map[x[1]] = [] rev_map[x[1]].append(x[0]) print('Loading Object Detector') det = ObjectDetector(objdetmodel) det.SCORE_THRESHOLD = 0.7 det.loadModel() # Warming up det.getBoundingBoxes(readImageFromFile(os.path.join(folder, rev_map[0][0]))) det.getBoundingBoxes(readImageFromFile(os.path.join(folder, rev_map[0][0]))) print('Loading Object Identifier') idn = SvmIdentifier(modelPath, det) idn.loadModel() if not idNum is None and not idn.isPresent(idNum): print( 'Provided Id {} is not present in trained model, will choose random Id' ) idNum = None if idNum is None: idNum = np.random.choice(list(idn.trained_ids))
print('Measuring performance over {} images...'.format(len(images))) config = args[1] weights = args[2] #model = YoloObjectDetector(config, weights, '../darknet/darknet/custom_data/detector.data') #model.loadModel() model = ObjectDetector(config) model.loadModel() print('Warming up...') #model.getBoundingBoxes(images[0]) #model.getBoundingBoxes(images[1]) model.getBoundingBoxes(cv2.imread(images[0])) model.getBoundingBoxes(cv2.imread(images[1])) [cv2.imread(x) for x in imagelist] print('Starting...') times = [] for im in imagelist: #model.getBoundingBoxes(im) img = cv2.imread(im) st = time.time() model.getBoundingBoxes(img) times.append(time.time() - st) print( 'Total time taken: {:.4f}s, Average time per detection: {:.4f}s, Min time: {:.4f}s, Max time: {:.4f}s' .format(sum(times), (sum(times)) / n, min(times), max(times)))
print('Running Detection on {} images'.format(n)) box_map = {} with open(args[2]) as f: box_map = json.load(f) f.close() print('Loading Object Detector...') det = ObjectDetector('ssd_quantized_1L_steps/saved_model') det.loadModel() #det.SCORE_THRESHOLD = 0.25 #det.IOU_THRESHOLD = 0.01 print('Warming up model...') det.getBoundingBoxes(cv2.imread(imageFiles[0])) det.getBoundingBoxes(cv2.imread(imageFiles[0])) batchSize = 10 if 'jaguar' in folder.lower(): expected_classid = 1 # Only tigers elif 'elp' in folder.lower() or 'elephant' in folder.lower(): expected_classid = 2 elif 'amur' in folder.lower(): expected_classid = 3 mAP = 0. numBoxes = 0 wrong = 0 correct = 0 start = time.time()
rootCa=rootCa, private_key=private_key, certificate=certificate) if not shadowClient: print('Failed to connect to AWS IOT device') exit(1) deviceShadow = shadowClient.createShadowHandlerWithName(device, False) CAMERA_IP = '<CAMERA IP>:<PORT>' print('Loading Model...') det = ObjectDetector('ssd/saved_model') det.loadModel() print('Model Load completed...') print('Warming up Model...') det.getBoundingBoxes(readImage(ip=CAMERA_IP)) det.getBoundingBoxes(readImage(ip=CAMERA_IP)) classes = loadClasses('ssd/label_mapping.csv') app = Flask(__name__, static_url_path='') if __name__ == '__main__': image = readImage(ip=CAMERA_IP) numPersons = countPeople(image) print('Found {} people in the image!'.format(numPersons)) @app.route('/') def loadUi(): return app.send_static_file('index.html')
print('Registered with MQTT server, publishing messages on channel: {}'. format(DEVICE_NAME)) with picamera.PiCamera(resolution=(640, 480), framerate=30) as camera: #camera.start_recording('my_video.h264') with picamera.array.PiRGBArray(camera) as stream: camera.start_preview() time.sleep(2) # Camera warmup for _ in camera.capture_continuous(stream, format='rgb'): image = stream.array #image = image[:,:,(2,1,0)] stream.truncate() stream.seek(0) start = time.time() results, image = detector.getBoundingBoxes(image) if len(results) > 0: # publisher.publishDetection(results) sendImageAsync(imagePublisher, image, results) print('Got results: {}, Time: {:.4f}s'.format( results, time.time() - start)) if display: image = cv2.resize( image, (detector.IMG_HEIGHT, detector.IMG_WIDTH)) if len(results) > 0: drawBbOnImage(image, results) cv2.imshow('test', image) cv2.waitKey(1)
exit(1) folder = args[1] imageFiles = [ '{}/{}'.format(folder, x) for x in os.listdir(folder) if x.endswith('.jpg') ] n = min(100, len(imageFiles)) print('Loading Model...') det = ObjectDetector('ssd/saved_model') det.loadModel() print('Warming up model...') # Run a dummy test once for warmup img = cv2.imread(imageFiles[0]) det.getBoundingBoxes(img) det.getBoundingBoxes(img) batchTimes = [] lower = 10 upper = 11 if len(args) == 4: lower = int(args[2]) upper = int(args[3]) f = open('test_times.csv', 'w') f.write('Batch Size,Total Time,Average Time\n') for i in range(lower, upper, 5): batchSize = i # Detect batchSize images at a time