def main(): rospy.init_node('object_detector', anonymous=True) detector = object_detector.ObjectDetector( rospy.get_param('~laser_topic', '/robot_0/base_scan')) rospy.spin()
def gen(fr): detector = object_detector.ObjectDetector( 'ssd_mobilenet_v1_coco_2017_11_17') #detector = ObjectDetector('mask_rcnn_inception_v2_coco_2018_01_28') #detector = ObjectDetector('pet', label_file='data/pet_label_map.pbtxt') #cam = camera.VideoCamera() retry_cnt = 0 while True: try: frame, face_result_list = fr.get_frame_live() #frame = cam.get_frame() frame, obj_detect_dict = detector.detect_objects_live( frame) self.buffer_handle(face_result_list, obj_detect_dict) ret, jpg = cv2.imencode('.jpg', frame) jpg_bytes = jpg.tobytes() yield (b'--frame\r\n' b'Content-Type: image/jpeg\r\n\r\n' + jpg_bytes + b'\r\n\r\n') except: try: time.sleep(1) if retry_cnt > 5: break fr = face_recog.FaceRecog() except: retry_cnt += 1
def test_max_resultsss_option(self): """Test the max_results option.""" option = od.ObjectDetectorOptions(max_results=_MAX_RESULTS) detector = od.ObjectDetector(_MODEL_FILE, options=option) result = detector.detect(self.image) self.assertLessEqual(len(result), _MAX_RESULTS, 'Too many results returned.')
def test_deny_list(self): """Test the label_deny_list option.""" option = od.ObjectDetectorOptions(label_deny_list=_DENY_LIST) detector = od.ObjectDetector(_MODEL_FILE, options=option) result = detector.detect(self.image) for detection in result: label = detection.categories[0].label self.assertNotIn( label, _DENY_LIST, 'Label "{0}" found but in deny list.'.format(label))
def test_allow_list(self): """Test the label_allow_list option.""" option = od.ObjectDetectorOptions(label_allow_list=_ALLOW_LIST) detector = od.ObjectDetector(_MODEL_FILE, options=option) result = detector.detect(self.image) for detection in result: label = detection.categories[0].label self.assertIn( label, _ALLOW_LIST, 'Label "{0}" found but not in label allow list'.format(label))
def test_score_threshold_option(self): """Test the score_threshold option.""" option = od.ObjectDetectorOptions(score_threshold=_SCORE_THRESHOLD) detector = od.ObjectDetector(_MODEL_FILE, options=option) result = detector.detect(self.image) for detection in result: score = detection.categories[0].score self.assertGreaterEqual( score, _SCORE_THRESHOLD, 'Detection with score lower than threshold found. {0}'.format( detection))
def main(): stream = io.BytesIO() camera = picamera.PiCamera() camera.resolution = (1024, 768) camera.capture(stream, format='jpeg') stream.seek(0) image = Image.open(stream) IMAGE_DIR = "/host/images/" FILENAME = "image1.jpeg" OUTPUT_IMAGE = IMAGE_DIR + "od2-" + FILENAME od = object_detector.ObjectDetector() op = od.detect(image) op.save(OUTPUT_IMAGE)
def _create_groud_truth_csv(self, output_file=_GROUND_TRUTH_FILE): """A util function to recreate the ground truth result.""" detector = od.ObjectDetector(_MODEL_FILE) result = detector.detect(self.image) with open(output_file, 'w') as f: header = ['label', 'left', 'top', 'right', 'bottom', 'score'] writer = csv.DictWriter(f, fieldnames=header) writer.writeheader() for d in result: writer.writerow({ 'label': d.categories[0].label, 'left': d.bounding_box.left, 'top': d.bounding_box.top, 'right': d.bounding_box.right, 'bottom': d.bounding_box.bottom, 'score': d.categories[0].score, })
def test_default_option(self): """Check if the default option works correctly.""" detector = od.ObjectDetector(_MODEL_FILE) result = detector.detect(self.image) # Check if all ground truth detection is found. for gt_detection in self._ground_truth_detections: is_gt_found = False for real_detection in result: is_label_match = real_detection.categories[ 0].label == gt_detection.categories[0].label is_bounding_box_match = self._iou( real_detection.bounding_box, gt_detection.bounding_box) > _BBOX_IOU_THRESHOLD # If a matching detection is found, stop the loop. if is_label_match and is_bounding_box_match: is_gt_found = True break # If no matching detection found, fail the test. self.assertTrue(is_gt_found, '{0} not found.'.format(gt_detection))
def main(duration): stream = io.BytesIO() camera = picamera.PiCamera() camera.resolution = (400, 300) IMAGE_DIR = "/host/sticker/staging/images/" FILENAME = "image.jpg" OUTPUT_IMAGE = IMAGE_DIR + FILENAME od = object_detector.ObjectDetector() while(1): time.sleep(duration) camera.capture(stream, format='jpeg') stream.seek(0) image = Image.open(stream) t1 = time.time() op = od.detect(image) op.save(OUTPUT_IMAGE) #image.save(OUTPUT_IMAGE) t2 = time.time() tdiff = t2 - t1 print(str(tdiff) + " - Wrote to " + OUTPUT_IMAGE) stream.seek(0) stream.truncate()
def start(self, evaluator, popsize, generations, max_motor_speed, foraging): if self.use_hyperneat: # HYPERNEAT NOT USED genotype = lambda innovations={}: NEATGenotype() else: genotype = lambda innovations={}: NEATGenotype( inputs=task.inputs, outputs=task.outputs, weight_range=task.weight_range, types=task.types, innovations=innovations, feedforward=task.feedforward, prob_add_node=task.prob_add_node, prob_add_conn=task.prob_add_conn, prob_mutate_weight=task.prob_mutate_weight, prob_reset_weight=task.prob_reset_weight, prob_reenable_conn=task.prob_reenable_conn, prob_disable_conn=task.prob_disable_conn, prob_reenable_parent=task.prob_reenable_parent, prob_mutate_bias=task.prob_mutate_bias, prob_mutate_response=task.prob_mutate_response, prob_mutate_type=task.prob_mutate_type, stdev_mutate_weight=task.stdev_mutate_weight, stdev_mutate_bias=task.stdev_mutate_bias, stdev_mutate_response=task.stdev_mutate_response, phys_dis_neat=task.phys_dis_neat, max_depth=task.max_depth, max_nodes=task.max_nodes, response_default=task.response_default, initial_weight_stdev=task.initial_weight_stdev, bias_as_node=task.bias_as_node, distance_excess=task.distance_excess, distance_disjoint=task.distance_disjoint, distance_weight=task.distance_weight) pop = NEATPopulation( genotype, popsize=evaluator.popsize, elitism=evaluator.elitism, compatibility_threshold=evaluator.compatibility_threshold, compatibility_threshold_delta=evaluator. compatibility_threshold_delta, target_species=evaluator.target_species, min_elitism_size=evaluator.min_elitism_size, young_age=evaluator.young_age, prob_mutate=evaluator.prob_mutate, young_multiplier=evaluator.young_multiplier, stagnation_age=evaluator.stagnation_age, old_age=evaluator.old_age, old_multiplier=evaluator.old_multiplier, tournament_selection_k=evaluator.tournament_selection_k, reset_innovations=evaluator.reset_innovations, survival=evaluator.survival, phys_dis_neat=evaluator.phys_dis_neat, sim_dis_neat=evaluator.sim_dis_neat, ip_address=self.ip) log = {'parameters': {}, 'generations': []} # log neat settings. log['parameters'] = { 'max_speed': max_motor_speed, 'inputs': evaluator.inputs, 'outputs': evaluator.outputs, 'weight_range': evaluator.weight_range, 'types': evaluator.types, 'feedforward': evaluator.feedforward, 'prob_add_node': evaluator.prob_add_node, 'prob_add_conn': evaluator.prob_add_conn, 'prob_mutate_weight': evaluator.prob_mutate_weight, 'prob_reset_weight': evaluator.prob_reset_weight, 'prob_reenable_conn': evaluator.prob_reenable_conn, 'prob_disable_conn': evaluator.prob_disable_conn, 'prob_reenable_parent': evaluator.prob_reenable_parent, 'prob_mutate_bias': evaluator.prob_mutate_bias, 'prob_mutate_response': evaluator.prob_mutate_response, 'prob_mutate_type': evaluator.prob_mutate_type, 'stdev_mutate_weight': evaluator.stdev_mutate_weight, 'stdev_mutate_bias': evaluator.stdev_mutate_bias, 'stdev_mutate_response': evaluator.stdev_mutate_response, 'phys_dis_neat': evaluator.phys_dis_neat, 'max_depth': evaluator.max_depth, 'max_nodes': evaluator.max_nodes, 'response_default': evaluator.response_default, 'initial_weight_stdev': evaluator.initial_weight_stdev, 'bias_as_node': evaluator.bias_as_node, 'distance_excess': evaluator.distance_excess, 'distance_disjoint': evaluator.distance_disjoint, 'distance_weight': evaluator.distance_weight, 'popsize': evaluator.popsize, 'elitism': evaluator.elitism, 'compatibility_threshold': evaluator.compatibility_threshold, 'compatibility_threshold_delta': evaluator.compatibility_threshold_delta, 'target_species': evaluator.target_species, 'min_elitism_size': evaluator.min_elitism_size, 'young_age': evaluator.young_age, 'prob_mutate ': evaluator.prob_mutate, 'young_multiplier': evaluator.young_multiplier, 'stagnation_age': evaluator.stagnation_age, 'old_age': evaluator.old_age, 'old_multiplier': evaluator.old_multiplier, 'tournament_selection_k': evaluator.tournament_selection_k, 'reset_innovations': evaluator.reset_innovations, 'survival': evaluator.survival, 'phys_dis_neat': evaluator.phys_dis_neat, 'sim_dis_neat': evaluator.sim_dis_neat, 'ip_address': self.ip } log['parameters'].update(self.evaluator.logs()) dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) bus = dbus.SessionBus() thymioController = dbus.Interface( bus.get_object('ch.epfl.mobots.Aseba', '/'), dbus_interface='ch.epfl.mobots.AsebaNetwork') thymioController.LoadScripts(AESL_PATH, reply_handler=dbusReply, error_handler=dbusError) # switch thymio LEDs off thymioController.SendEventName('SetColor', [0, 0, 0, 0], reply_handler=dbusReply, error_handler=dbusError) # thresholds are set > SHOULD BE IN FORAGING if foraging == True: self.detector = object_detector.ObjectDetector( 0.4, 0.01, thymioController) self.evaluator.logger.info( str(self.ip), 'Puck_threshold:' + str(self.detector.has_puck_threshold)) self.evaluator.logger.info( str(self.ip), 'Goal_threshold:' + str(self.detector.has_goal_threshold2)) task = self.evaluator task.set_thymio_controller(thymioController) ctrl_serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) ctrl_serversocket.bind((self.ip, 1337)) ctrl_serversocket.listen(5) ctrl_client = None img_serversocket = None img_client = None def set_client(): global ctrl_client print 'Control server: waiting for socket connections...' (ctrl_client, address) = ctrl_serversocket.accept() task.set_ctrl_client(ctrl_client) print 'Control server: got connection from', address thread.start_new_thread(set_client, ()) if self.use_img_client: img_serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) img_serversocket.bind((self.ip, 31337)) img_serversocket.listen(5) def set_img_client(): global img_client print 'Image server: waiting for socket connections...' (img_client, address) = img_serversocket.accept() print 'Image server: got connection from', address write_header(img_client) thread.start_new_thread(set_img_client, ()) def epoch_callback(population): # update log dump population_backup = population.giveBackUp() species_backup = population.giveBackUpSpecies() generation = { 'individuals': [], 'phenotypes': [], 'gen_number': population.generation } for individual in population_backup: copied_connections = { str(key): value for key, value in individual.conn_genes.items() } generation['individuals'].append({ 'node_genes': deepcopy(individual.node_genes), 'conn_genes': copied_connections, 'stats': deepcopy(individual.stats) }) if self.use_hyperneat: for phenotype in population.phenotype_backup: generation['phenotypes'].append({ 'cm': deepcopy(phenotype.cm), 'act': deepcopy(phenotype.act) }) generation['species_id'] = [ species.id for species in species_backup ] generation['species_size'] = [ len(species.members) for species in species_backup ] log['generations'].append(generation) #task.getLogger().info(', '.join([str(ind.stats['fitness']) for ind in population_backup.population])) outputDir = os.path.join(OUTPUT_PATH, self.experiment_name) if population.generation == 1: self.firstdate = time.strftime("%d-%m-%y_%H-%M") date = time.strftime("%d-%m-%y_%H-%M") jsonLogFilename = os.path.join( outputDir, self.experiment_name + '_' + date + '.json') with open(jsonLogFilename, 'w') as f: #print(outputDir, self.experiment_name + '_' + date + '.json') json.dump(log, f, cls=CustomEncoder) # update clean file summary filename = os.path.join( outputDir, self.experiment_name + '_' + self.firstdate + '_cleanlog.txt') #print filename index = 1 for individual in population_backup: msg = "%d\t%d\t%f\t%d\t%d\t%d\t%d\t%d\t%d\t" % ( population.generation, index, individual.stats['fitness'], len(individual.node_genes), len(individual.conn_genes), individual.stats['specieid'], individual.stats['id'], individual.stats['parent1'], individual.stats['parent2']) index += 1 with open(filename, 'a') as f: f.write(msg + "\n") try: evaluator = lambda evaluee: task.evaluate(NeuralNetwork(evaluee)) \ if hasattr(evaluee, 'get_network_data') \ else task.evaluate(evaluee) converter = hn.create_converter( task.substrate()) if self.use_hyperneat else lambda x: x pop.epoch(generations=task.generations, evaluator=evaluator, solution=evaluator, callback=epoch_callback, converter=converter) except KeyboardInterrupt: release_resources(task.thymioController, ctrl_serversocket, ctrl_client, img_serversocket, img_client) sys.exit(1) release_resources(task.thymioController, ctrl_serversocket, ctrl_client, img_serversocket, img_client) sys.exit(0)
from datetime import datetime def load_image_into_numpy_array(image): (im_width, im_height) = image.size return np.array(image.getdata()).reshape( (im_height, im_width, 3)).astype(np.uint8) PATH_TO_TEST_IMAGES_DIR = '../models/research/object_detection/test_images' TEST_IMAGE_PATHS = [os.path.join(PATH_TO_TEST_IMAGES_DIR, 'image{}.jpg'.format(i)) for i in range(1, 3)] # Size, in inches, of the output images. IMAGE_SIZE = (12, 8) dect = object_detector.ObjectDetector('..') graph1 = dect.get_graph() with graph1.as_default(): sess = tf.Session() for image_path in TEST_IMAGE_PATHS: image = Image.open(image_path) # the array based representation of the image will be used later in order to prepare the # result image with boxes and labels on it. image_np = load_image_into_numpy_array(image) a = datetime.now() output_dict = dect.run_inference_for_single_image(image_np, sess) print datetime.now() - a print(output_dict)