def main(): """ Full Jetson Detector Program """ parser = argparse.ArgumentParser(description="Inference Program") parser.add_argument( '--config_file', help= f'json configuration file containint params to initialize the jetson', type=str) args = parser.parse_args() try: global objectDetector if not os.path.exists(args.config_file): raise ValueError( f'Cannot find configuration file "{args.config_file}"') with open(args.config_file, 'r') as f: robotJetsonConfiguration = json.load(f) log.warning(LOGGER_OBJECT_DETECTOR_STARTUP, msg='Launching object detector now.') loop = asyncio.get_event_loop() # loop.set_debug(enabled=True) loop.set_exception_handler(handle_exception) log.warning(LOGGER_OBJECT_DETECTOR_STARTUP, msg='Launching runner.') signals = (signal.SIGINT, signal.SIGTERM) for s in signals: loop.add_signal_handler(s, lambda s=s: loop.create_task( objectDetector.graceful_shutdown(s))) objectDetector = ObjectDetector(robotJetsonConfiguration, loop) loop.create_task(objectDetector.run()) loop.run_forever() except SystemExit: log.info(LOGGER_OBJECT_DETECTOR_STARTUP, 'Caught SystemExit...') except Exception: log.critical(LOGGER_OBJECT_DETECTOR_STARTUP, 'Crash in startup : {}'.format(traceback.print_exc())) finally: # loop.stop() loop.close() logging.shutdown()
class TrafficSignDetector(object): def __init__(self): self.detector = ObjectDetector() self.classifier = Classifier() def detect(self, image): objects = time.measure(lambda: self.detector.predict(image), 'detection') extend_bounding_boxes(objects, 0.15) images = time.measure( lambda: prepare_for_classification(objects, image), 'image preprocessing') labels = time.measure(lambda: self.classifier.predict(images), 'classification') print(objects, labels) return objects, labels def detect_multiple(self, images): objects, preprocessed_images, box_scales = time.measure( lambda: self.detector.predict_multiple(images), 'detection') if len(objects[0]) == 0: return [[]], [[]] preprocessed = time.measure( lambda: resize_for_classification(objects, preprocessed_images, images), 'preprocessing') labels = time.measure(lambda: self.classifier.predict(preprocessed), 'classification') results = [] j = 0 for i in range(0, len(images)): results.append([labels[k] for k in range(j, j + len(objects[i]))]) j += len(objects[i]) r_objects = [] for i in range(0, len(objects)): r_objects.append([]) objs = objects[i] r_objects[i] = [[obj[0], *obj[1:] * box_scales[i]] for obj in objs] print(r_objects, results) return r_objects, results
def __init__(self, model_path, model_name): self.model_name = model_name nms_thresh = 0.3 conf_thresh = 0.5 cfg_path = glob(os.path.join(model_path, '*cfg'))[0] weight_path = glob(os.path.join(model_path, '*weights'))[0] self.det = ObjectDetector(cfg_path, weight_path, conf_thresh, nms_thresh, 0) name_path = glob(os.path.join(model_path, '*names'))[0] self.class_names = open(name_path).read().splitlines()
def main(model_number): model = models[model_number] cfg = get_config(model['model_cfg']) detector = ObjectDetector( cfg, os.path.join( 'resource', model['checkpoint'] ), device_id=0 ) image = cv2.imread('image.jpg') for _ in trange(NUM_WARM, desc='Warm-up'): detector.predict(image, threshold=0.3) start = time.time() for _ in trange(NUM_RUNS, desc='Benches'): detector.predict(image, threshold=0.3) torch.cuda.synchronize() total = time.time() - start print(f'Performance: {NUM_RUNS / total:0.2f} rps')
def main(args): # 1) get value from sample calibration image files_to_check = HT.getFilesInDirectory(args['directory'], '.jpg') logger.info(f"Looking at {len(files_to_check)} jpg images") results = [] # option to use calibration if args['use_calibration']: calib = CI.Image.open(args['calibration_image']) calib_result = SD.calibratePlaque(calib) area = calib_result.get('contour_area') # ratio = calib_result.get('ratio') bl, tl, tr, br = calib_result.get('bl_tl_tr_br') # 3) loop over images in a directory, outputting the names of positive and negative images # TODO: below steps can be optimized with text recognition happpening over the returned list # we get a dict of file names, with each filename having a list of metadata for f in tqdm(files_to_check, desc=f"finding plaques with area {area}"): results.extend( SD.get_plaques_matching_ratio( f, good_area=area, save_directory=args['save_directory'], _debug_mode=args['debug'], cutoff_ratio=float(args['cutoff_ratio']))) # option to use object detection elif args['use_hog']: detector = ObjectDetector(loadPath=args["detector"]) for f in tqdm(files_to_check, desc="finding plaques with HOG"): plaque_details_list = SD.get_plaques_with_hog( f, hog=detector, save_directory=args['save_directory'], _debug_mode=args['debug']) logger.debug( f"How many results for {f}: {len(plaque_details_list)}") results.extend(plaque_details_list) # 4) after successfull plaque grabbing, use open and image stuff to get a bounding box around just the words, and send that to ocr # for idx, meta in tqdm(enumerate(results), desc="reading text from cropped images"): # results[idx].text, results[idx].thresheld_image = TR.tess_from_image(results[idx].image) # 5) plot the results # plot_multiple_images(results) # with open('/home/johnny/Documents/performance_metrics_3-6/hog_results.pkl', 'wb') as p: # pickle.dump(results, p) # 6) evaluate performance for images with matching calb image and those with mismatched calibrtation image evaluate_performance(results)
def detector_thread(object_detector: ObjectDetector, to_detect: Queue, to_track: Queue, progressbar: tqdm): classes = load_classes("detector/yolo/data/coco.names") while True: if object_detector.stop: break if to_detect.empty(): sleep(1) continue task = to_detect.get() if task.message == QueueMessage.DONE: break progressbar.update(1) img_tensor = task.payload detections = object_detector.detect_in_image_tensor(img_tensor) detected_vehicles = [] if detections is not None: for x1, y1, x2, y2, conf, cls_conf, cls_pred in detections: class_name = classes[int(cls_pred)] bounding_box = BoundingBox(top_left=Point(x=int(x1), y=int(y1)), bottom_right=Point(x=int(x2), y=int(y2))) raw_data = [x1, y1, x2, y2, conf] if class_name in ["car", "bus", "truck"]: detected_vehicles.append( DetectedObject(bounding_box, class_name, raw_data)) to_detect.task_done() if len(detected_vehicles) > 0: new_task = Task(payload=detected_vehicles) to_track.put(new_task) finishing_task = Task(message=QueueMessage.DONE) to_track.put(finishing_task)
#!/usr/bin/python3 from flask import Flask from flask_restplus import Api, Resource, fields, reqparse from werkzeug.contrib.fixers import ProxyFix from werkzeug.datastructures import FileStorage from tempfile import NamedTemporaryFile import io from PIL import Image import time import os import numpy as np from detector import ObjectDetector global detector detector = ObjectDetector() ALLOWED_MIMETYPES = set(['image/png', 'image/jpeg']) app = Flask(__name__) app.config['SWAGGER_UI_JSONEDITOR'] = True app.config['MAX_CONTENT_LENGTH'] = 2 * 1024 * 1024 # 2MB app.wsgi_app = ProxyFix(app.wsgi_app) api = Api(app, version='0.1', title='Objects Detector API', description='Detect objects in images') ns = api.namespace('detector', description='Object detection operations') image_upload = reqparse.RequestParser()
from detector import Darknet_ObjectDetector as ObjectDetector from detector import DetBBox import requests from PIL import Image from PIL import ImageFilter from StringIO import StringIO def _get_image(url): return Image.open(StringIO(requests.get(url).content)) if __name__ == '__main__': from PIL import Image voc_names = ["aeroplane", "bicycle", "bird", "boat", "bottle","bus", "car", "cat", "chair", "cow", "diningtable","dog", "horse", "motorbike", "person", "pottedplant","sheep", "sofa", "train", "tvmonitor"] det = ObjectDetector('../cfg/yolo.cfg','../yolo.weights') url = 'http://farm9.staticflickr.com/8323/8141398311_2fd0af60f7.jpg' #for i in xrange(4): rst, run_time = det.detect_object(_get_image(url)) print 'got {} objects in {} seconds'.format(len(rst), run_time) for bbox in rst: print '{} {} {} {} {} {}'.format(voc_names[bbox.cls], bbox.top, bbox.left, bbox.bottom, bbox.right, bbox.confidence)
from detector import ObjectDetector import sys from PIL import Image import numpy as np img_path = sys.argv[1] img = np.asarray(Image.open(img_path), dtype=np.uint8) # Provide the .pb model file path graph_path = "./faster_rcnn_resnet50_coco_2018_01_28/frozen_inference_graph.pb" model = ObjectDetector(graph_path) out = model.run(img) print(out)
def image_callback(self, img, camera_info): """Image detector""" # img = self.cv_bridge.imgmsg_to_cv2(image_msg, "rgb8") # cv2.imwrite('img.png', cv2.cvtColor(img, cv2.COLOR_BGR2RGB)) # for debugging # Update camera matrix and distortion coefficients if self.input_is_rectified: P = np.matrix(camera_info.P, dtype='float64') P.resize((3, 4)) camera_matrix = P[:, :3] dist_coeffs = np.zeros((4, 1)) else: camera_matrix = np.matrix(camera_info.K, dtype='float64') camera_matrix.resize((3, 3)) dist_coeffs = np.matrix(camera_info.D, dtype='float64') dist_coeffs.resize((len(camera_info.D), 1)) # Downscale image if necessary height, width, _ = img.shape scaling_factor = float(self.downscale_height) / height if scaling_factor < 1.0: camera_matrix[:2] *= scaling_factor img = cv2.resize( img, (int(scaling_factor * width), int(scaling_factor * height))) for m in self.models: self.pnp_solvers[m].set_camera_intrinsic_matrix(camera_matrix) self.pnp_solvers[m].set_dist_coeffs(dist_coeffs) # Copy and draw image img_copy = img.copy() im = Image.fromarray(img_copy) draw = Draw(im) # detection_array = Detection3DArray() # detection_array.header = image_msg.header detection_array = [] for m in self.models: # Detect object results = ObjectDetector.detect_object_in_image( self.models[m].net, self.pnp_solvers[m], img, self.config_detect) # Publish pose and overlay cube on image for i_r, result in enumerate(results): if result["location"] is None: continue loc = result["location"] ori = result["quaternion"] # transform orientation transformed_ori = tf3d.quaternions.qmult( ori, self.model_transforms[m]) # rotate bbox dimensions if necessary # (this only works properly if model_transform is in 90 degree angles) dims = rotate_vector(vector=self.dimensions[m], quaternion=self.model_transforms[m]) dims = np.absolute(dims) dims = tuple(dims) # TODO: Convert pose_msg to detection x = loc[0] / CONVERT_SCALE_CM_TO_METERS y = loc[1] / CONVERT_SCALE_CM_TO_METERS z = loc[2] / CONVERT_SCALE_CM_TO_METERS qw = transformed_ori[3] qx = transformed_ori[0] qy = transformed_ori[1] qz = transformed_ori[2] detection = np.array([x, y, z, qw, qx, qy, qz]) detection_array.append((m, detection)) # pose_msg = PoseStamped() # pose_msg.header = image_msg.header # CONVERT_SCALE_CM_TO_METERS = 100 # pose_msg.pose.position.x = loc[0] / CONVERT_SCALE_CM_TO_METERS # pose_msg.pose.position.y = loc[1] / CONVERT_SCALE_CM_TO_METERS # pose_msg.pose.position.z = loc[2] / CONVERT_SCALE_CM_TO_METERS # pose_msg.pose.orientation.x = transformed_ori[0] # pose_msg.pose.orientation.y = transformed_ori[1] # pose_msg.pose.orientation.z = transformed_ori[2] # pose_msg.pose.orientation.w = transformed_ori[3] # Publish # self.pubs[m].publish(pose_msg) # self.pub_dimension[m].publish(str(dims)) # Add to Detection3DArray # detection = Detection3D() # hypothesis = ObjectHypothesisWithPose() # hypothesis.id = self.class_ids[result["name"]] # hypothesis.score = result["score"] # hypothesis.pose.pose = pose_msg.pose # detection.results.append(hypothesis) # detection.bbox.center = pose_msg.pose # detection.bbox.size.x = dims[0] / CONVERT_SCALE_CM_TO_METERS # detection.bbox.size.y = dims[1] / CONVERT_SCALE_CM_TO_METERS # detection.bbox.size.z = dims[2] / CONVERT_SCALE_CM_TO_METERS # detection_array.detections.append(detection) # Draw the cube if None not in result['projected_points']: points2d = [] for pair in result['projected_points']: points2d.append(tuple(pair)) draw.draw_cube(points2d, self.draw_colors[m]) return detection_array
from detector import ObjectDetector import cv2 import argparse ap = argparse.ArgumentParser() ap.add_argument("-d", "--detector", required=True, default='detector.svm', help="path to trained detector to load...") ap.add_argument("-i", "--image", required=True, help="path to an image for object detection...") ap.add_argument("-a", "--annotate", default=None, help="text to annotate...") args = vars(ap.parse_args()) detector = ObjectDetector(load_path=args["detector"]) imagePath = args["image"] image = cv2.imread(imagePath) detector.detect(image, annotate=args["annotate"])
from detector import ObjectDetector import numpy as np import cv2 import argparse ap = argparse.ArgumentParser() ap.add_argument("-d", "--detector", required=True, help="path to trained detector to load...") #ap.add_argument("-i","--image",required=True,help="path to an image for object detection...") ap.add_argument("-a", "--annotate", default=None, help="text to annotate...") args = vars(ap.parse_args()) detector = ObjectDetector(loadPath=args["detector"]) cap = cv2.VideoCapture("naspati.webm") grabbed, image = cap.read() while True: grabbed, image = cap.read() x, y, xb, yb = detector.detect(image, annotate=args["annotate"]) #detector.detect(image,annotate=args["annotate"]) center = [[(x / 2 + xb / 2) / 2], [(y + yb) / 3]] key = cv2.waitKey(1) & 0xFF # if the 'q' key is pressed, stop the loop if key == ord("q"):
def __init__(self): global test_mode """ コンストラクタ。引数をパースする。また、カメラ、画像判定モデルを初期化する。 """ # (1)引数をパースする arg_parser = argparse.ArgumentParser(description='Detecting Camera') arg_parser.add_argument('target_name', help='Target name to detecting. Split by commas.') arg_parser.add_argument('-x', '--exclude', default=None, help='Target excluded from detection. Split by commas.') arg_parser.add_argument('-i', '--interval', type=int, default=60, help='Camera shooting interval[sec] (default:60)') arg_parser.add_argument('-p', '--period', type=int, default=0, help='Shooting period[min]. Infinite if 0(default).') arg_parser.add_argument('-d', '--directory', default='./', help='Output directory (default:current dir)') arg_parser.add_argument('-l', '--list', action='store_true', default=False, help='List target names.') arg_parser.add_argument('-s', '--saveimage', action='store_true', default=False, help='Save image mode.(default:Off)') arg_parser.add_argument('-t', '--tweet', action='store_true', default=False, help='Tweet detected.(default:Off)') arg_parser.add_argument('-e', '--test', action='store_true', default=False, help='Test mode.(default:Off)') args = arg_parser.parse_args() # パースした引数の内容を変数で覚えておく。 self._target_names = args.target_name.split(',') self._exclude_names = args.exclude.split(',') if args.exclude is not None else [] self._output_directory = args.directory self._period_sec = args.period * 60 self._interval_sec = args.interval self._list_option = args.list self._save_image_mode = args.saveimage self._tweet_mode = args.tweet self._test_mode = args.test # 検知器を用意し、利用可能なターゲット名称をそこから取得する。 self._detector = ObjectDetector() available_names = self._detector.list_names() # 引数"-l"が指定されていたら、利用可能なターゲット名称をリストアップして終了 if self._list_option: print('Available names:') print(', '.join(available_names)) exit(0) # (2)ターゲット名が、利用可能なターゲット名称に含まれるかチェック。 # なければプログラム終了。 for name in self._target_names + self._exclude_names: if name not in available_names: print('"{0}" is not available word. Retry input.'.format(name)) print('(Search available words for -l options.)') exit(0) # カメラの無い環境であればテストモードを強制的にONにする。 if without_camera: self._test_mode = True # (3)引数の内容を表示。 print(('Camera started.\n' ' - Target name : {0}\n' ' - Exclude name : {1}\n' ' - Interval : {2}\n' ' - Shooting period : {3}\n' ' - Output directory: {4}\n' ' - Save image mode : {5}\n' ' - Tweet mode : {6}\n' ' - Test mode : {7}\n' ).format(','.join(self._target_names), ','.join(self._exclude_names), str(self._interval_sec) + '[sec]', str(args.period) + '[min]' if self._period_sec != 0 else 'infinite', self._output_directory, self._save_image_mode, self._tweet_mode, self._test_mode) ) # (4)画像判定モデルを初期化。 print('Start model loading...') self._detector.open() print('Finish.') # (5)Twitter接続を初期化。 if self._tweet_mode: self._twitter = Twitter()
from detector import ObjectDetector import numpy as np import cv2 import argparse detector_path = r'd:\Users\boykni\Desktop\object_detection\Object-Detector-master\detector.svm' #annotations_path = r"d:\Users\boykni\Desktop\object_detection\Object-Detector-master\annot.npy" images_path = r"\\Mos-srv1\Petroview\NAWAT\Completion Data\LA_COMPLETION_REPORTS\imageclassifier\training_dataset\titlepage\3_242294_2_1.jpg" detector = ObjectDetector(loadPath=detector_path) imagePath = images_path image = cv2.imread(imagePath) detector.detect(image,annotate="LOGO")
class DetectionCamera: """ 検知カメラ。指定した物体をカメラが写したときに画像を保存することを繰り返す。 """ def __init__(self): global test_mode """ コンストラクタ。引数をパースする。また、カメラ、画像判定モデルを初期化する。 """ # ロガーを初期化する logging.config.fileConfig('logging.ini') self.logger = getLogger(__name__) # 引数をパースする arg_parser = argparse.ArgumentParser(description='Detecting Camera') arg_parser.add_argument( 'target_name', help='Target name to detecting. Split by commas.') arg_parser.add_argument( '-x', '--exclude', default=None, help='Target excluded from detection. Split by commas.') arg_parser.add_argument( '-i', '--interval', type=int, default=60, help='Camera shooting interval[sec] (default:60)') arg_parser.add_argument( '-p', '--period', type=int, default=0, help='Shooting period[min]. Infinite if 0(default).') arg_parser.add_argument('-d', '--directory', default='./', help='Output directory (default:current dir)') arg_parser.add_argument('-l', '--list', action='store_true', default=False, help='List target names.') arg_parser.add_argument('-s', '--saveimage', action='store_true', default=False, help='Save image mode.(default:Off)') arg_parser.add_argument('-t', '--tweet', action='store_true', default=False, help='Tweet detected.(default:Off)') arg_parser.add_argument('-e', '--test', action='store_true', default=False, help='Test mode.(default:Off)') args = arg_parser.parse_args() # パースした引数の内容を変数で覚えておく。 self._target_names = args.target_name.split(',') self._exclude_names = args.exclude.split( ',') if args.exclude is not None else [] self._output_directory = args.directory self._period_sec = args.period * 60 self._interval_sec = args.interval self._list_option = args.list self._save_image_mode = args.saveimage self._tweet_mode = args.tweet self._test_mode = args.test # 検知器を用意し、利用可能なターゲット名称をそこから取得する。 self._detector = ObjectDetector() available_names = self._detector.list_names() # 引数"-l"が指定されていたら、利用可能なターゲット名称をリストアップして終了 if self._list_option: print('Available names:') print(', '.join(available_names)) exit(0) # ターゲット名が、利用可能なターゲット名称に含まれるかチェック。 # なければプログラム終了。 for name in self._target_names + self._exclude_names: if name not in available_names: print('"{0}" is not available word. Retry input.'.format(name)) print('(Search available words for -l options.)') exit(0) # カメラの無い環境であればテストモードを強制的にONにする。 if without_camera: self._test_mode = True # 引数の内容を表示。 self.logger.info( ('Camera started.\n' ' - Target name : {0}\n' ' - Exclude name : {1}\n' ' - Interval : {2}\n' ' - Shooting period : {3}\n' ' - Output directory: {4}\n' ' - Save image mode : {5}\n' ' - Tweet mode : {6}\n' ' - Test mode : {7}\n').format( ','.join(self._target_names), ','.join(self._exclude_names), str(self._interval_sec) + '[sec]', str(args.period) + '[min]' if self._period_sec != 0 else 'infinite', self._output_directory, self._save_image_mode, self._tweet_mode, self._test_mode)) # 画像判定モデルを初期化。 self.logger.info('Start model loading...') self._detector.open() self.logger.info('Finish.') # Twitter接続を初期化。 if self._tweet_mode: self._twitter = Twitter() self._face_detector = FaceDetector() def run(self): """ カメラ処理。撮影、画像判定、ファイル保存を繰り返す。 """ self.logger.info('Start shooting.') # 処理開始時間を保持。このあと経過時間を計算するため。 start_time = datetime.datetime.now() # カメラを初期化。テストモードならスキップ。 if not self._test_mode: camera = picamera.PiCamera() camera.resolution = CAMERA_RESOLUTION camera.hflip = True camera.vflip = True ''' # カメラ設定用のオプション。必要に応じてコメント外にコピーすること。 #https://picamera.readthedocs.io/en/release-0.2/api.html camera.sharpness = 0 # -100 to 100 camera.contrast = 0 # -100 to 100 camera.brightness = 50 # 0 to 100 camera.saturation = 0 camera.ISO = 0 camera.video_stabilization = False camera.exposure_compensation = 0 camera.exposure_mode = 'auto' camera.meter_mode = 'average' camera.awb_mode = 'auto' camera.image_effect = 'none' camera.color_effects = None camera.rotation = 0 camera.hflip = False camera.vflip = False camera.crop = (0.0, 0.0, 1.0, 1.0) ''' # 以下、撮影期間が終了するまで繰り返し while True: now_time = datetime.datetime.now() # CPU温度をチェック。冷えている場合のみ撮影・投稿処理を続ける。 if not self._is_cpu_hot(): # カメラ画像を取得。 if not self._test_mode: camera.capture('tmp.jpg') # 画像に目的のものが写っていればTwitterに投稿する。 self.try_tweet_image(now_time) # 記録用にCPU温度を出力しておく。 self._is_cpu_hot() # 経過時間をチェック。オプションで指定した時間異常が過ぎていたら処理を停止する。 elapsed_time = now_time - start_time elapsed_sec = elapsed_time.total_seconds() if self._period_sec != 0 and elapsed_sec < self._period_sec: self.logger.info('Shooting period ({0} sec) ended.'.format( self._period_sec)) break # インターバル期間の分、待ち。 wait_time = self._interval_sec - (elapsed_sec % self._interval_sec) time.sleep(wait_time) def try_tweet_image(self, now_time): """ 画像に目的のものが写っていればTwitterに投稿する。 :param now_time: 現在時刻 :return: 写っていた物の名称 """ # 目的のものが写っているか判定。写っていなければ終了。 matched_name, matched_box = self._match_target_image( 'tmp.jpg', threshold=SCORE_THRESHOLD) duration = datetime.datetime.now() - now_time self.logger.info('Detect {0}. ({1:.1f}[sec])'.format( matched_name, duration.total_seconds())) if matched_name is None: return None # 正規化座標をピクセル座標に変換する org_image = Image.open('tmp.jpg') org_width, org_height = org_image.size ymin_n, xmin_n, ymax_n, xmax_n = matched_box px_box = (int(xmin_n * org_width), int(xmax_n * org_width), int(ymin_n * org_height), int(ymax_n * org_height)) # 顔が検知枠の中に含まれているかチェックする。含まれていなければ終了。 contain_face = self._face_detector.contains_face('tmp.jpg', px_box) self.logger.info(' Contain face: {0}'.format(contain_face)) if not contain_face: return matched_name # 目的の物の位置を中心にカメラ画像を切り取る。 self._crop_center(px_box, 'tmp.jpg', 'crop.jpg') # 切り取り画像をTwitterに投稿。Tweet modeがONの場合のみ。 if self._tweet_mode: self._twitter.post(matched_name, 'crop.jpg') # 画像を保存。 # 切り取り画像は"{指定されたディレクトリ}/{年月日}_{時分秒}_{物体名}.jpg"とし、 # 検知枠の付いた判定画像は末尾を"_result.jpg"として保存する。 if self._save_image_mode: now_str = now_time.strftime('%Y%m%d_%H%M%S') original_file_name = '{0}_{1}.jpg'.format(now_str, matched_name) original_file_path = os.path.join(self._output_directory, original_file_name) shutil.copy('crop.jpg', original_file_path) result_file_name = '{0}_{1}_result.jpg'.format( now_str, matched_name) result_file_path = os.path.join(self._output_directory, result_file_name) shutil.copy('result.jpg', result_file_path) return matched_name def _is_cpu_hot(self): """ CPUが規定の温度より熱くなっているかを返す :return: THERMAL_LIMITより熱ければTrue """ # CPU温度の出力されたファイルから温度の値を取得する。 # OSの違いなどでファイルが無い場合は、Falseを返して終了。 if not os.path.exists(THERMAL_FILE): self.logger.info( 'CPU Thermal file does not exist. File:{0}'.format( THERMAL_FILE)) return False with open(THERMAL_FILE, 'r') as file: thermal = int(file.read()) # ファイルには1000倍した整数値が記述されているので、1000で割る。 thermal /= 1000 # THERMAL_LIMITより熱いかチェックし、返す。ログも出力する。 result = (thermal > THERMAL_LIMIT) result_text = 'hot' if result else 'cool' self.logger.info('Thermal: {0}\'C => {1}'.format(thermal, result_text)) return result def _match_target_image(self, img_path, threshold=0): """ 引数の画像に何が写っているか判定し、検知対象であればその物体名を返す。 :param img_path: 確認したい画像 :return: 物体名 検知対象でなければNone """ # 画像を判定 results = self._detector.detect(img_path) # 判定結果のチェック # 全画像分の判定結果について、写っている物体がターゲット名と同じかチェックし、 # スコアが最高のものだけを記録する。 # またターゲット除外名ものがあれば、即座に中断する。 max_score_name = None max_score_box = None max_score = 0 for name, score, box in zip(results['names'], results['scores'], results['boxes']): # ターゲット除外名であれば即座に終了。 if name in self._exclude_names: self.logger.info('Exclude name "{0}" detected.'.format(name)) return None, None # ターゲットであれば、今までの最高スコアと比較し、最高スコアを更新したら記録する。 if name in self._target_names: if score > max_score: max_score = score max_score_name = name max_score_box = box # 最高スコアの物を返す。 return max_score_name, max_score_box def _crop_center(self, px_box, original_image_path, crop_image_path): """ matched_boxと中央が合う位置で、original_image_pathの画像を切り取り、その結果をcrop_image_pathのファイルに保存する。 :param px_box: ピクセル座標での検知枠 :param original_image_path: 元画像のパス :param crop_image_path: 切り取った画像を保存するパス """ # 元画像を読み込む。 org_image = Image.open(original_image_path) org_width, org_height = org_image.size # 検知枠の座標を取り出す。 xmin, xmax, ymin, ymax = px_box # 検知枠と中心点が合うような切り取り枠の位置を求める。 x_center = (xmin + xmax) / 2 y_center = (ymin + ymax) / 2 crop_width, crop_height = IMAGE_SIZE xmin_c = round(x_center - (crop_width / 2)) ymin_c = round(y_center - (crop_height / 2)) # 切り取り枠が元画像からはみ出ていたら中央側に寄せる。 if xmin_c < 0: xmin_c = 0 if ymin_c < 0: ymin_c = 0 if xmin_c + crop_width > org_width: xmin_c = org_width - crop_width if ymin_c + crop_height > org_height: ymin_c = org_height - crop_height # 切り取り枠で、元画像を切り取った結果の画像を生成し、保存する。 crop_image = org_image.crop( (xmin_c, ymin_c, xmin_c + crop_width, ymin_c + crop_height)) crop_image.save(crop_image_path)
from PIL import Image from PIL import ImageFilter from StringIO import StringIO def _get_image(path): #return Image.open(StringIO(requests.get(url).content)) return Image.open(path) if __name__ == '__main__': from PIL import Image voc_names = [ "aeroplane", "bicycle", "bird", "boat", "bottle", "bus", "car", "cat", "chair", "cow", "diningtable", "dog", "horse", "motorbike", "person", "pottedplant", "sheep", "sofa", "train", "tvmonitor", "plate_license" ] det = ObjectDetector('../cfg/yolo-plate.cfg', '/home/himon/code/yolo/yolo-plate_final.weights') #url = 'http://farm9.staticflickr.com/8323/8141398311_2fd0af60f7.jpg' path = '/home/himon/code/yolo/cars.jpg' #for i in xrange(4): rst, run_time = det.detect_object(_get_image(path)) print 'got {} objects in {} seconds'.format(len(rst), run_time) for bbox in rst: print '{} {} {} {} {} {}'.format(voc_names[bbox.cls], bbox.top, bbox.left, bbox.bottom, bbox.right, bbox.confidence)
def __init__(self): self.detector = ObjectDetector() self.classifier = Classifier()
img_path = os.path.join(os.path.dirname(__file__), "world", "render_files") file_list = sorted(os.listdir(img_path)) for f in file_list: img = cv.imread(os.path.join(img_path, f)) vid.write(img) cv.destroyAllWindows() vid.release() if __name__ == "__main__": img_difficulty = Difficulty.MEDIUM_FAST original, new, binary, color_matrix = load_image(img_difficulty) detector = ObjectDetector(new, binary, color_matrix) detector.scan_image() # All we need to supply to the world objects = detector.get_objects_array() objects_dict = detector.get_objects() label_plane = detector.get_label_plane() # detector.print_label_plane() # call world World.get_instance().create_world(objects=objects, objects_dict=objects_dict, label_plane=label_plane) # Discover properties of objects
from detector import ObjectDetector import numpy as np import argparse ap = argparse.ArgumentParser() ap.add_argument("-a", "--annotations", required=True, default='annot.npy', help="path to saved annotations...") ap.add_argument("-i", "--images", required=True, default='images.npy', help="path to saved image paths...") ap.add_argument("-d", "--detector", default='detector.svm', help="path to save the trained detector...") args = vars(ap.parse_args()) print "[INFO] loading annotations and images" annotations = np.load(args["annotations"]) image_paths = np.load(args["images"]) detector = ObjectDetector() print "[INFO] creating & saving object detector" detector.fit(image_paths, annotations, visualize=True, save_path=args["detector"])
def image_callback( self, img, camera_info, img_name="00000.png", # this is the name of the img file to save, it needs the .png at the end output_folder='out_inference', # folder where to put the output ): img_name = str(img_name).zfill(5) """Image callback""" # img = self.cv_bridge.imgmsg_to_cv2(image_msg, "rgb8") # cv2.imwrite('img.png', cv2.cvtColor(img, cv2.COLOR_BGR2RGB)) # for debugging # Update camera matrix and distortion coefficients if self.input_is_rectified: P = np.matrix(camera_info['projection_matrix']['data'], dtype='float64').copy() P.resize((3, 4)) camera_matrix = P[:, :3] dist_coeffs = np.zeros((4, 1)) else: # TODO camera_matrix = np.matrix(camera_info.K, dtype='float64') camera_matrix.resize((3, 3)) dist_coeffs = np.matrix(camera_info.D, dtype='float64') dist_coeffs.resize((len(camera_info.D), 1)) # Downscale image if necessary height, width, _ = img.shape scaling_factor = float(self.downscale_height) / height if scaling_factor < 1.0: camera_matrix[:2] *= scaling_factor img = cv2.resize( img, (int(scaling_factor * width), int(scaling_factor * height))) for m in self.models: self.pnp_solvers[m].set_camera_intrinsic_matrix(camera_matrix) self.pnp_solvers[m].set_dist_coeffs(dist_coeffs) # Copy and draw image img_copy = img.copy() im = Image.fromarray(img_copy) draw = Draw(im) # dictionary for the final output dict_out = {"camera_data": {}, "objects": []} for m in self.models: # Detect object results, beliefs = ObjectDetector.detect_object_in_image( self.models[m].net, self.pnp_solvers[m], img, self.config_detect) # print(results) # print('---') # continue # Publish pose and overlay cube on image for i_r, result in enumerate(results): if result["location"] is None: continue # print(result) loc = result["location"] ori = result["quaternion"] print(loc) dict_out['objects'].append({ 'class': m, 'location': np.array(loc).tolist(), 'quaternion_xyzw': np.array(ori).tolist(), 'projected_cuboid': np.array(result['projected_points']).tolist(), }) # print( dict_out ) # transform orientation # TODO # transformed_ori = tf.transformations.quaternion_multiply(ori, self.model_transforms[m]) # rotate bbox dimensions if necessary # (this only works properly if model_transform is in 90 degree angles) # dims = rotate_vector(vector=self.dimensions[m], quaternion=self.model_transforms[m]) # dims = np.absolute(dims) # dims = tuple(dims) # Draw the cube if None not in result['projected_points']: points2d = [] for pair in result['projected_points']: points2d.append(tuple(pair)) draw.draw_cube(points2d, self.draw_colors[m]) # save the output of the image. im.save(f"{output_folder}/{img_name}.png") # save the json files with open(f"{output_folder}/{img_name.replace('png','json')}", 'w') as fp: json.dump(dict_out, fp)
import cv2 from centroidtracker import CentroidTracker from detector import ObjectDetector import numpy as np import tensorflow as tf from stream import ThreadedCam from trackedobjects import TrackableObject from sort import Sort from time import time from numba import jit from utils import roiselector det = ObjectDetector('cardetector') ct = CentroidTracker() trackers = [] totalframes = 0 skipframes = 5 trackedobjects = {} roiwindow = 'Pls Select ROI' H = None W = None def check_and_correct_boundaries(x,y,xmin,ymin,xmax,ymax): if x < xmin: x = xmin elif x > xmax: x = xmax-1 if y < ymin: y = ymin
parser.add_argument('--name-path', type=str, default=defaults['name_path']) parser.add_argument('--cfg-path', type=str, default=defaults['cfg_path']) parser.add_argument('--weight-path', type=str, default=defaults['weight_path']) parser.add_argument('--thresh', type=float, default=0.5) parser.add_argument('--nms', type=float, default=0.4) parser.add_argument('--draw', action='store_true') return parser.parse_args() if __name__ == "__main__": args = parse_args() ObjectDetector.set_device(0) det = ObjectDetector(args.cfg_path, args.weight_path, args.thresh, args.nms, int(args.draw)) class_names = open(args.name_path).read().splitlines() print class_names start = time() c_load_time, c_pred_time = 0, 0 for i, im_name in enumerate(sys.stdin): py_all_time = time() - start py_load_time = py_all_time - c_load_time - c_pred_time print >> sys.stderr, "%d | pyall %f | pyload %f | cload %f | cpred %f" % ( i, py_all_time, py_load_time, c_load_time, c_pred_time) try: im_name = im_name.strip() img = Image.open(im_name)
from detector import ObjectDetector import numpy as np import cv2 import argparse ap = argparse.ArgumentParser() ap.add_argument("-d", "--detector", required=True, help="path to trained detector to load...") ap.add_argument("-i", "--image", required=True, help="path to an image for object detection...") ap.add_argument("-a", "--annotate", default=None, help="text to annotate...") args = vars(ap.parse_args()) detector = ObjectDetector(loadPath=args["detector"]) imagePath = args["image"] image = cv2.imread(imagePath) detector.detect(image, annotations=args["annotate"])
from detector import ObjectDetector import numpy as np import cv2 import argparse import imutils ap = argparse.ArgumentParser() ap.add_argument("-d", "--detector", required= True, help="path to trained detector to load...") ap.add_argument("-v", "--video", required=True, help="path to a video for object detection...") ap.add_argument("-a","--annotate", default=None, help="text to annotate...") args = vars(ap.parse_args()) detector = ObjectDetector(loadPath=args["detector"]) vs = cv2.VideoCapture(args["video"]) while(vs.isOpened()): ret, imagePath = vs.read() imagePath = imutils.resize(imagePath, width=min(400, imagePath.shape[1])) image =[] image ==imagePath if imagePath is None: break #image = imagePath detector.detectV(imagePath, annotate=args["annotate"]) #cv2.imshow("Bead Detector", image)
class DetectionCamera: """ 検知カメラ。指定した物体をカメラが写したときに画像を保存することを繰り返す。 """ def __init__(self): global test_mode """ コンストラクタ。引数をパースする。また、カメラ、画像判定モデルを初期化する。 """ # (1)引数をパースする arg_parser = argparse.ArgumentParser(description='Detecting Camera') arg_parser.add_argument('target_name', help='Target name to detecting. Split by commas.') arg_parser.add_argument('-x', '--exclude', default=None, help='Target excluded from detection. Split by commas.') arg_parser.add_argument('-i', '--interval', type=int, default=60, help='Camera shooting interval[sec] (default:60)') arg_parser.add_argument('-p', '--period', type=int, default=0, help='Shooting period[min]. Infinite if 0(default).') arg_parser.add_argument('-d', '--directory', default='./', help='Output directory (default:current dir)') arg_parser.add_argument('-l', '--list', action='store_true', default=False, help='List target names.') arg_parser.add_argument('-s', '--saveimage', action='store_true', default=False, help='Save image mode.(default:Off)') arg_parser.add_argument('-t', '--tweet', action='store_true', default=False, help='Tweet detected.(default:Off)') arg_parser.add_argument('-e', '--test', action='store_true', default=False, help='Test mode.(default:Off)') args = arg_parser.parse_args() # パースした引数の内容を変数で覚えておく。 self._target_names = args.target_name.split(',') self._exclude_names = args.exclude.split(',') if args.exclude is not None else [] self._output_directory = args.directory self._period_sec = args.period * 60 self._interval_sec = args.interval self._list_option = args.list self._save_image_mode = args.saveimage self._tweet_mode = args.tweet self._test_mode = args.test # 検知器を用意し、利用可能なターゲット名称をそこから取得する。 self._detector = ObjectDetector() available_names = self._detector.list_names() # 引数"-l"が指定されていたら、利用可能なターゲット名称をリストアップして終了 if self._list_option: print('Available names:') print(', '.join(available_names)) exit(0) # (2)ターゲット名が、利用可能なターゲット名称に含まれるかチェック。 # なければプログラム終了。 for name in self._target_names + self._exclude_names: if name not in available_names: print('"{0}" is not available word. Retry input.'.format(name)) print('(Search available words for -l options.)') exit(0) # カメラの無い環境であればテストモードを強制的にONにする。 if without_camera: self._test_mode = True # (3)引数の内容を表示。 print(('Camera started.\n' ' - Target name : {0}\n' ' - Exclude name : {1}\n' ' - Interval : {2}\n' ' - Shooting period : {3}\n' ' - Output directory: {4}\n' ' - Save image mode : {5}\n' ' - Tweet mode : {6}\n' ' - Test mode : {7}\n' ).format(','.join(self._target_names), ','.join(self._exclude_names), str(self._interval_sec) + '[sec]', str(args.period) + '[min]' if self._period_sec != 0 else 'infinite', self._output_directory, self._save_image_mode, self._tweet_mode, self._test_mode) ) # (4)画像判定モデルを初期化。 print('Start model loading...') self._detector.open() print('Finish.') # (5)Twitter接続を初期化。 if self._tweet_mode: self._twitter = Twitter() def run(self): """ カメラ処理。撮影、画像判定、ファイル保存を繰り返す。 """ print('Start shooting.') # (1)処理開始時間を保持。このあと経過時間を計算するため。 start_time = datetime.datetime.now() # (2)カメラを初期化。テストモードならスキップ。 if not self._test_mode: camera = picamera.PiCamera() camera.resolution = CAMERA_RESOLUTION #camera.hflip = True #camera.vflip = True ''' # カメラ設定用のオプション。必要に応じてコメント外にコピーすること。 #https://picamera.readthedocs.io/en/release-0.2/api.html camera.sharpness = 0 # -100 to 100 camera.contrast = 0 # -100 to 100 camera.brightness = 50 # 0 to 100 camera.saturation = 0 camera.ISO = 0 camera.video_stabilization = False camera.exposure_compensation = 0 camera.exposure_mode = 'auto' camera.meter_mode = 'average' camera.awb_mode = 'auto' camera.image_effect = 'none' camera.color_effects = None camera.rotation = 0 camera.hflip = False camera.vflip = False camera.crop = (0.0, 0.0, 1.0, 1.0) ''' # 以下、撮影期間が終了するまで繰り返し counter = 1 while True: now_time = datetime.datetime.now() # (3)カメラ画像を取得。 if not self._test_mode: camera.capture('tmp.jpg') # (4)目的のものが写っているか判定。 matched_name, matched_box = self._match_target_image('tmp.jpg', threshold=SCORE_THRESHOLD) duration = datetime.datetime.now() - now_time print('[{0}] {1} - Detect {2}. ({3:.1f}[sec])'.format(counter, now_time.strftime('%Y/%m/%d %H:%M:%S'), matched_name, duration.total_seconds())) if matched_name is not None: # (5)写っていたら画像をTwitterに投稿。Tweet modeがONの場合のみ。 if self._tweet_mode: self._twitter.post(matched_name, 'tmp.jpg') # (6)画像を保存。 # 元画像は"{指定されたディレクトリ}/{年月日}_{時分秒}_{物体名}.jpg"とし、 # 検知枠の付いた画像は末尾を"_result.jpg"として保存する。 if self._save_image_mode: now_str = now_time.strftime('%Y%m%d_%H%M%S') original_file_name = '{0}_{1}.jpg'.format(now_str, matched_name) original_file_path = os.path.join(self._output_directory, original_file_name) shutil.copy('tmp.jpg', original_file_path) result_file_name = '{0}_{1}_result.jpg'.format(now_str, matched_name) result_file_path = os.path.join(self._output_directory, result_file_name) shutil.copy('result.jpg', result_file_path) # (7)経過時間をチェック。オプションで指定した時間異常が過ぎていたら処理を停止する。 elapsed_time = now_time - start_time elapsed_sec = elapsed_time.total_seconds() if self._period_sec != 0 and elapsed_sec < self._period_sec: print('Shooting period ({0} sec) ended.'.format(self._period_sec)) break # (8)インターバル期間の分、待ち。 wait_time = self._interval_sec - (elapsed_sec % self._interval_sec) time.sleep(wait_time) counter += 1 def _match_target_image(self, img_path, threshold=0): """ 引数の画像に何が写っているか判定し、検知対象であればその物体名を返す。 :param img_path: 確認したい画像 :return: 物体名 検知対象でなければNone """ # (1) 画像を判定 results = self._detector.detect(img_path) # (2) 判定結果のチェック # 全画像分の判定結果について、写っている物体がターゲット名と同じかチェックし、 # スコアが最高のものだけを記録する。 # またターゲット除外名ものがあれば、即座に中断する。 max_score_name = None max_score_box = None max_score = 0 for name, score, box in zip(results['names'], results['scores'], results['boxes']): # ターゲット除外名であれば即座に終了。 if name in self._exclude_names: print('Exclude name "{0}" detected.'.format(name)) return None, None # ターゲットであれば、今までの最高スコアと比較し、最高スコアを更新したら記録する。 if name in self._target_names: if score > max_score: max_score = score max_score_name = name max_score_box = box # 最高スコアの物を返す。 return max_score_name, max_score_box
from detector import ObjectDetector import numpy as np import argparse ap = argparse.ArgumentParser() ap.add_argument("-a", "--annotations", required=True, help="path to saved annotations...") ap.add_argument("-i", "--images", required=True, help="path to saved image paths...") ap.add_argument("-d", "--detector", default=None, help="path to save the trained detector...") args = vars(ap.parse_args()) print("[INFO] loading annotations and images") annots = np.load(args["annotations"]) imagePaths = np.load(args["images"]) detector = ObjectDetector() print("[INFO] creating & saving object detector") detector.fit(imagePaths, annots, visualize=False, savePath=args["detector"])
def image_callback( self, img, camera_info, P_matrix, img_name="00000.png", # this is the name of the img file to save, it needs the .png at the end output_folder='out_inference', # folder where to put the output showbelief=False, gt_keypoints=None, ): # img_name = str(img_name).zfill(5)+'.png' """Image callback""" # img = self.cv_bridge.imgmsg_to_cv2(image_msg, "rgb8") # cv2.imwrite('img.png', cv2.cvtColor(img, cv2.COLOR_BGR2RGB)) # for debugging # Update camera matrix and distortion coefficients if self.input_is_rectified: ''' P = np.matrix(camera_info['camera_matrix']['data'], dtype='float64').copy() # print(P) P.resize((3, 3)) ''' P = np.matrix(P_matrix, dtype='float64').copy() P.resize((3, 3)) camera_matrix = P dist_coeffs = np.zeros((4, 1)) # print(camera_matrix) # raise() else: # TODO camera_matrix = np.matrix(camera_info.K, dtype='float64') camera_matrix.resize((3, 3)) dist_coeffs = np.matrix(camera_info.D, dtype='float64') dist_coeffs.resize((len(camera_info.D), 1)) # add padding to the image img = cv2.copyMakeBorder(img, 0, self.padding_height, 0, self.padding_width, cv2.BORDER_CONSTANT, (0, 0, 0)) # Downscale image if necessary height, width, _ = img.shape scaling_factor = float(self.downscale_height) / height if scaling_factor < 1.0: camera_matrix[:2] *= scaling_factor img = cv2.resize( img, (int(scaling_factor * width), int(scaling_factor * height))) for m in self.models: if 'ensemble' not in m: self.pnp_solvers[m].set_camera_intrinsic_matrix(camera_matrix) self.pnp_solvers[m].set_dist_coeffs(dist_coeffs) # Copy and draw image img_copy = img.copy() im = Image.fromarray(img_copy) draw = Draw(im) # dictionary for the final output dict_out = {"camera_data": {}, "objects": []} beliefs_outputs = [] Metric = {} for m in self.models: # Detect object if 'ensemble' in m: if 'full' in self.keypoints_ensemble_1.net_path: full = True else: full = False else: if 'full' in self.models[m].net_path: full = True else: full = False Score = [] Location = [] Orientation = [] Uncertainty = [] Reprojection_error = [] if 'visii' in m: new = False visii_flag = True else: new = False visii_flag = False if 'ensemble' in m: results, beliefs = ObjectDetector.detect_object_in_image( self.keypoints_ensemble_1.net, self.pnp_solvers[self.m1], img, self.config_detect, full=full, grid_belief_debug=showbelief, run_sampling=self.config_detect.run_sampling, new=new, second_model=self.keypoints_ensemble_2.net, visii=visii_flag) else: results, beliefs = ObjectDetector.detect_object_in_image( self.models[m].net, self.pnp_solvers[m], img, self.config_detect, full=full, grid_belief_debug=showbelief, run_sampling=self.config_detect.run_sampling, new=new, visii=visii_flag) beliefs_outputs.append(beliefs) # print(results) # print('---') # continue # Publish pose and overlay cube on image for i_r, result in enumerate(results): if result["location"] is None: continue loc = result["location"] ori = result["quaternion"] # print(loc) Score.append(result['score']) Location.append(loc) Orientation.append(ori) Uncertainty.append(result['uncertainty']) # Compute the reprojection error projected_cuboid = result['projected_points'] raw_cuboid = result['raw_points'] reprojection_error = 0 for i in range(9): temp1 = np.array(projected_cuboid[i]) temp2 = np.array(raw_cuboid[i]) if raw_cuboid[i] is not None: reprojection_error += np.linalg.norm(temp1 - temp2) Reprojection_error.append(reprojection_error) dict_out['objects'].append({ 'class': m, 'location': np.array(loc).tolist(), 'quaternion_xyzw': np.array(ori).tolist(), 'projected_cuboid': np.array(result['projected_points']).tolist(), 'confidence': np.array(result['confidence']).tolist(), 'raw_cuboid': np.array(result['raw_points']).tolist(), 'reprojection_error': reprojection_error.tolist() }) # print( dict_out ) # transform orientation # TODO # transformed_ori = tf.transformations.quaternion_multiply(ori, self.model_transforms[m]) # rotate bbox dimensions if necessary # (this only works properly if model_transform is in 90 degree angles) # dims = rotate_vector(vector=self.dimensions[m], quaternion=self.model_transforms[m]) # dims = np.absolute(dims) # dims = tuple(dims) # Draw the cube if None not in result['projected_points']: points2d = [] for pair in result['projected_points']: points2d.append(tuple(pair)) draw.draw_cube(points2d, self.draw_colors[m], text=str(i_r + 1)) # draw the raw prediction points text = 0 for p in result['raw_points']: # draw.draw_dot(p,self.draw_colors[m], text=str(text)) #debug draw.draw_dot(p, self.draw_colors[m]) text += 1 # draw the ground truth keypoints if not gt_keypoints is None: text = 0 for p in gt_keypoints: draw.draw_dot(p, 'black', text=str(text)) text += 1 if len(Score) > 0: best_index = 0 for index in range(len(Score)): # Using score to select the best candidate # if Score[index] > Score[best_index]: # Using reprojection_error to select the best candidate if Reprojection_error[index] < Reprojection_error[ best_index]: best_index = index Metric[m] = {} if len(Score) > 0: Metric[m]['centroid score'] = Score[best_index] Metric[m]['uncertainty'] = np.array(Uncertainty[best_index]) Metric[m]['uncertainty'][:3] = 0.01 * Metric[m][ 'uncertainty'][:3] # cm -> m Metric[m]['location'] = 0.01 * np.array( Location[best_index]) # cm -> m Metric[m]['orientation'] = np.array(Orientation[best_index]) else: Metric[m]['centroid score'] = 0 Metric[m]['uncertainty'] = np.array( [1000, 1000, 1000, 1000, 1000, 1000, 1000]) Metric[m]['location'] = None Metric[m]['orientation'] = None # draw the single object uncertainty # draw_uncertainty(Score, uncertainty, f"{output_folder}/uncertainty_{img_name}") draw_uncertainty_multi( img_name.replace('.png', ''), Metric, f"{output_folder}/{img_name.replace('.png','.jpg')}", colors=self.draw_colors) # print (result) # save the output of the image. if not opt.json_only: im.save(f"{output_folder}/{img_name}") # save the json files path_json = f"{output_folder}/{img_name.replace('png','')}json" if os.path.exists(path_json): # with open(path_json) as f: # data = json.load(f) # data['objects'] = data['objects'] + dict_out['objects'] with open(path_json, 'w') as fp: json.dump(dict_out, fp, indent=4) else: with open(path_json, 'w') as fp: json.dump(dict_out, fp, indent=4) return im, beliefs_outputs, Metric
from detector import ObjectDetector from centroidtracker import CentroidTracker import cv2 import numpy as np import tensorflow as tf import stream det = ObjectDetector('facedetector') # tracker = CentroidTracker() # cap = stream.ThreadedCam('samples/traffic.mp4',fps=30).start() cap = stream.ThreadedCam(0).start() with tf.Session(graph=det.graph) as sess: while 2: frame = cap.get_frame() frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) boxes = det.detect_boxes_and_scores(frame, sess, sort=True) print(boxes) # print(objects) # for objid,centroid in objects.items(): # y,x = centroid # text = "ID {}".format(objid) # cv2.putText(frame, text, (int(x) - 10, int(y) - 10),cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2) # cv2.circle(frame,(int(x),int(y)),radius=5,color=[0,255,255],thickness=-1) cv2.imshow("Test", cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)) if cv2.waitKey(1) & 0xFF == ord('q'): break cap.stop() cv2.destroyAllWindows()
parse.add_argument("-i", "--images", help="Path to saved images", default="train_images.npy") parse.add_argument("-d", "--detector", help="Path to save model", default="svm_model.svm") parse.add_argument("-ta", "--test_annotate", help="Path to saved test boxes annotations", default="test_annot.npy") parse.add_argument("-tim", "--test_images", help="Path to test images", default="test_images.npy") args = vars(parse.parse_args()) annots = np.load(args["annotations"]) imagePaths = np.load(args["images"]) trainAnnot = np.load(args["test_annotate"]) trainImages = np.load(args["test_images"]) detector = ObjectDetector() detector.fit(imagePaths, annots, trainAnnot, trainImages, visualize=True, savePath=args["detector"])
import imutils import datetime import time ap = argparse.ArgumentParser() ap.add_argument("-d", "--detector", required=True, help="path to trained detector to load...") ap.add_argument("-i", "--image", required=True, help="path to an image for object detection...") ap.add_argument("-a", "--annotate", default=None, help="text to annotate...") args = vars(ap.parse_args()) detector = ObjectDetector(loadPath=args["detector"]) imagePath = args["image"] image = cv2.imread(imagePath) video_capture = cv2.VideoCapture('aLine.mp4') while (True): ret, frame = video_capture.read() detector.detectsp(frame, annotate=args["annotate"]) time.sleep(0.001) if cv2.waitKey(1) & 0xFF == ord('q'): break detector.detect(image, annotate=args["annotate"])