def collect_samples(): cap = cv2.VideoCapture(0) img_process = ImageProcessor() count = -1 frames = [] while True: print(f"Count = {count}") _, frame = cap.read() frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) frame = img_process.horizontal_flip(frame) cv2.imshow('Frame', frame) key = cv2.waitKey(1) if key == ord('s'): count = 1 if count > 0 and count <= SAMPLES_TO_COLLECT: frames.append(frame) count += 1 elif count > SAMPLES_TO_COLLECT: break if key & 0xFF == ord('q'): break ind = 0 while True: cv2.imshow('Part', frames[ind]) key = cv2.waitKey(1) if key == ord('s'): ind += 1
def __init__(self, brain, eps_start, eps_end, eps_steps, thread_delay, env_name, gamma, gamma_n, n_step_return, render=False): threading.Thread.__init__(self) self.render = render self.env = gym.make(env_name) self.brain = brain self.agent = Agent(eps_start, eps_end, eps_steps, self.env.action_space.n, gamma, gamma_n, n_step_return, self.brain) self.eps_start = eps_start self.eps_end = eps_end self.thread_delay = thread_delay self.dims = (84, 84) self.current_state = np.zeros((4, self.dims[1], self.dims[0]), dtype=np.uint8) self.image_processor = ImageProcessor() self.fire = 1 #fix to make generic self.true_done = False self.lives = 0 self.total_ep_reward = 0
def __init__(self): self.image_processor = ImageProcessor(crop_face=CROP_FACES) self.model_manager = ModelManager() # initialize volatile variables self.prediction_model = MobilenetPredictor() self.personal_trainer = PersonalTrainer() self.sessions = {}
def __init__(self, webcam_w=800, webcam_h=600, min_face_width=200): self.root = tkinter.Tk() self.root.title("Artificial Bartender") self.root.protocol("WM_DELETE_WINDOW", self.on_closing) self.root.bind("<Configure>", self.on_resize) self.vid_label = Label(self.root, anchor="center", background='black', width=webcam_w, height=webcam_h) self.vid_label.pack(expand=YES, fill=BOTH) self.window_width = self.webcam_w = webcam_w self.window_height = self.webcam_h = webcam_h self.image_ratio = float(webcam_w / webcam_h) self.min_face_width = min_face_width self.cap = None self.start_webcam() # Configuring text-to-speech self.tts_engine = pyttsx3.init() rate = self.tts_engine.getProperty('rate') self.tts_engine.setProperty('rate', rate - 60) self.image_processor = ImageProcessor('font.ttf') self.text_font_size = 20 self.text_font = ImageFont.truetype('font.ttf', self.text_font_size) self.last_phrase_time = time() self.process_webcam() self.root.mainloop()
def process_images(params): img_path = params processor = ImageProcessor(os.path.join(args.data_dir, 'images'), os.path.join(args.data_dir, 'masks'), CLASSES, overlap=args.overlap, window_size=args.window_size) return processor.process_image(img_path)
def __init__(self, main_window): self.main_window = main_window self.files_loader = FilesLoader(self.main_window) self.rgb_thr_processor = RGBThreshProcessor(self.main_window) self.image_processor = ImageProcessor(self.main_window, self)
def test_with_flushing_data_for_tests(self, source_image_storage, descriptor_search, feature_engine): source_image_storage.return_value.save_source_image.return_value = self.ref_source feature_engine.return_value.extract_features.return_value = self.list_image_regions descriptor_search.return_value.find_similar_for_region.return_value = expected_search_result ImageProcessor(flush_data=True) descriptor_search.assert_called_once_with(descriptor_shape=(48,), flush_data=True) source_image_storage.assert_called_once_with(flush_data=True)
def __init__(self, saved_state_file_path=None): self.saved_state_file_path = saved_state_file_path self.image_processor = ImageProcessor() self.on_data_change_handlers = [] self.on_data_change(self.persist_state) print(">> Loading state_file from '%s'" % saved_state_file_path) self.known_people = self.load_saved_state(saved_state_file_path) print("number of entries in known_people dataset: {}".format( len(self.known_people))) print("known_people dataset: {}".format(self.known_people))
def test_init(self): """ Just after instantiating the class, a matcher is not set and thus, an exception should be raised if matcher method was to be invoked. """ processor = ImageProcessor() with pytest.raises(Exception): processor.matcher()
def __init__(self, dataset, MAX_DISTANCE_THRESHOLD=0.2, training_samples_file_path=None): self.MAX_DISTANCE_THRESHOLD = MAX_DISTANCE_THRESHOLD self.dataset = dataset self.training_samples_file_path = training_samples_file_path self.image_processor = ImageProcessor() self.train_classifier() self.dataset.on_data_change(self.train_classifier)
def _image_characteristics(self, img_path): """Read the file and use the neural networks to predict the image characteristics. Assumes the training data is in the image-processing directory, with its default names. """ ip = ImageProcessor() category, category_name = ip.predict_category(img_path) attributes, attributes_names = ip.predict_attributes(img_path) return category, category_name, attributes, attributes_names
async def logo_detection(files: List[UploadFile] = File(...)): response = None try: image_processor = ImageProcessor() data = json.dumps({image_processor.detect_logos(files[0])}) response = Response(content=data, media_type="application/json") except Exception as error: logging.error("Error while uploading file ", str(error)) response = {"Error ": str(error)} return response
def __init__(self, width, height): pg.init() self.W = width self.H = height self.screen = pg.display.set_mode((self.W, self.H)) self.running = True self.scl = 40 self.cols = self.W // self.scl self.rows = self.H // self.scl self.grid = [[0 for i in range(self.cols)] for j in range(self.rows)] self.ip = ImageProcessor() self.model = ModelLoader('model/trained_model_2.pt').load_model()
def test_detect_logos(self): images = [ "altroz.png", "cred.png", "dream11.png", "paytm.png", "unacademy.png" ] image_processor = ImageProcessor() for image in images: detected_logos = image_processor.detect_logos(image) detected_logo_names = [logo['name'] for logo in detected_logos] brand_name = image.split(".")[0] self.assertIn(brand_name, detected_logo_names)
def __init__(self): self.sqs_queue_url = _get_environment_variable( key="DEMO_APP_SQS_URL", example_value= "https://sqs.eu-west-2.amazonaws.com/123456789000/ImageQueue") self.s3_bucket_name = _get_environment_variable( key="DEMO_APP_BUCKET_NAME", example_value="test-images-for-my-demo-app") self.task_publisher = TaskPublisher(self.sqs_queue_url, self.s3_bucket_name) self.image_processor = ImageProcessor(self.sqs_queue_url, self.s3_bucket_name)
def test_live(): # if True: # return cap = cv2.VideoCapture(0) while True: _, frame_orig = cap.read() frame_orig = ImageProcessor().horizontal_flip(frame_orig) cv2.imshow('Main', frame_orig) key = cv2.waitKey(1) if key & 0xFF == ord('q'): break
def get_image(): global arr ImgP = ImageProcessor() try: req = request.data except: print("Error occured! Couldn't get image data") ImgP.imageGenerator(req) arr = ImgP.imageProcessor() DigP = DigitPredictor() result_digit = DigP.digitPredict(arr) return jsonify(data=result_digit)
def test_live_effecient(square_dim=320): cap = cv2.VideoCapture(0) img_process = ImageProcessor() base_imgs = None test_max_black_pixel_count = 0 drum_area1 = DrumArea(top_left_corner=(100,10), square_dim=square_dim, sound='j') drum_area2 = DrumArea(top_left_corner=(100,320), square_dim=square_dim, sound='c') drum_areas = [drum_area1, drum_area2] area_listener = AreaListener(drum_areas = drum_areas) last_states = [False for i in range(len(drum_areas))] max_black_pixel = [0 for i in range(len(drum_areas))] while True: _, frame_orig = cap.read() frame_orig = img_process.horizontal_flip(frame_orig) area_listener.draw_area(frame_orig) if not base_imgs: area_listener.set_base_image(frame_orig) base_imgs = area_listener.get_base_imgs(resize_dim=RESIZE_DIM) target_areas = area_listener.get_all_target_areas(\ frame_orig, resize_dim=RESIZE_DIM) for i,target_area in enumerate(target_areas): diff = cv2.absdiff(target_area, base_imgs[i]) diff_gray = np.asarray(cv2.cvtColor(diff, cv2.COLOR_BGR2GRAY)) if test_max_black_pixel_count < 100: max_black_pixel[i] = max(max_black_pixel[i], max(diff_gray.flatten())) else: diff_gray[diff_gray > max_black_pixel[i]] = 255 diff_gray[diff_gray <= max_black_pixel[i]] = 0 num_whites = len(diff_gray[diff_gray == 255]) if num_whites > THRESHOLD_NUM_WHITES: if not last_states[i]: last_states[i] = True drum_areas[i].playSound() drum_areas[i].markPlayed(frame_orig) else: last_states[i] = False cv2.waitKey(1) test_max_black_pixel_count += 1 cv2.imshow('Main', frame_orig) key = cv2.waitKey(1) if key & 0xFF == ord('q'): break
def test_image_processor(): ip = ImageProcessor() test_img_paths = os.listdir('test_images') for i, img in enumerate(os.listdir(TEST_DIR)): typ = ip.predict_type(os.path.join(TEST_DIR, img)) result = "passed" if typ in ('Top', 'Bottom', 'Full-body') else "failed" print("Type prediction test %d %s" % ((i+1), result)) possible_attributes = get_list_of_attributes() for i, img in enumerate(os.listdir(TEST_DIR)): _, attrs = ip.predict_attributes(os.path.join(TEST_DIR, img)) result = "passed" if all(a in possible_attributes for a in attrs) else "failed" print("Attribute prediction test %d %s" % ((i+1), result))
def test_live(model): cap = cv2.VideoCapture(0) drum_area1 = DrumArea(top_left_corner=(50, 50), square_dim=320, sound='c') dareas = [drum_area1] area_listener = AreaListener(drum_areas=dareas) img_process = ImageProcessor() base_set = False base_imgs = None while True: _, frame_orig = cap.read() frame_orig = img_process.horizontal_flip(frame_orig) frame_color = frame_orig.copy() frame = frame_color.copy() if not base_set: area_listener.set_base_image(frame) base_imgs = area_listener2.get_base_imgs() base_set = True for drum_area in dareas: orig, target = drum_area.get_area(frame_orig), drum_area.base_img img_copy = orig.copy() diff = cv2.absdiff(target, orig) diff_gray = np.asarray(cv2.cvtColor(diff, cv2.COLOR_BGR2GRAY)) diff_gray = cv2.resize(diff_gray, (80, 80)) diff_gray = tf.reshape(diff_gray, [80, 80, 1]) center = model.predict(np.asarray([diff_gray.numpy() / 255.0]))[0] cv2.circle(img_copy, tuple(map(int, center)), 9, (30, 110, 200), -1) cv2.imshow('Pred', img_copy) cv2.waitKey(1) area_listener2.draw_area(frame_color) key = cv2.waitKey(1) cv2.imshow('Main', frame_color) cv2.waitKey(1) if key == ord('s'): print('resetting base') area_listener2.set_base_image(frame) base_imgs = area_listener2.get_base_imgs() if key & 0xFF == ord('q'): break
def _choose_processor(self, file_name, mime): """Select the right processor given the mime type.""" if re.match('.*?/pdf.*?', mime): self.logger.info("Pdf processor found!") pdf = PdfProcessor(file_name) return pdf if re.match('image/.*?', mime): self.logger.info("Image processor found!") return ImageProcessor(file_name) self.logger.error("Cannot process file with %s mime type." % mime) raise ApplicationError.UnsupportedFormat( "Cannot process file with %s mime type." % mime)
def __init__(self): self.nt = NetworkTable.getTable('/camera') #Cameras self.piston_cam = cs.UsbCamera('Piston Cam', 0) self.piston_cam.setVideoMode(cs.VideoMode.PixelFormat.kMJPEG, 160, 120, 35) #160 vs. 120 self.piston_cam.setExposureAuto() self.piston_cam.getProperty('backlight_compensation').set(5) #self.piston_cam.setExposureManual(35) #self.piston_cam.setBrightness(65) #test = self.piston_cam.getProperty("exposure_absolute") #print(self.piston_cam.getProperty("exposure_absolute")) self.piston_server = cs.MjpegServer('httpserver', 1181) self.piston_server.setSource(self.piston_cam) if self.secondary_cam: self.light_ring_cam = cs.UsbCamera('Light Ring Cam', 0) self.light_ring_cam.setVideoMode(cs.VideoMode.PixelFormat.kMJPEG, 320, 240, 20) # This only seems to affect automatic exposure mode # -> higher value means it lets more light in when facing a big light self.light_ring_cam.getProperty('backlight_compensation').set(5) #Image Processing self.cvsink = cs.CvSink('cvsink') self.cvsink.setSource(self.light_ring_cam) self.cvsource = cs.CvSource('cvsource', cs.VideoMode.PixelFormat.kMJPEG, 320, 240, 20) #Streaming Servers #self.ring_stream = cs.MjpegServer("ring server", 1182) #self.ring_stream.setSource(self.light_ring_cam) if self.STREAM_CV: self.cv_stream = cs.MjpegServer('cv stream', 1183) self.cv_stream.setSource(self.cvsource) #Blank mat self.img = np.zeros(shape=(320, 240, 3), dtype=np.uint8) self.processor = ImageProcessor()
def __init__(self): self.node_name = rospy.get_name() rospy.loginfo("[%s] Initializing......" % (self.node_name)) self.visualization = True self.image_msg = None self.processor = ImageProcessor() self.pub_detections = rospy.Publisher( "~image_processed", CompressedImage, queue_size=1) rospy.Service('~add_proc', AddProc, self.cbAddProc) rospy.Service('~exec_proc', GetString, self.cbExecProc) # self.sub_image = rospy.Subscriber("~image_raw", Image, self.cbImg , queue_size=1) self.sub_image = rospy.Subscriber( "~image_raw/compressed", CompressedImage, self.cbImg, queue_size=1) rospy.loginfo("[%s] Initialized." % (self.node_name))
def __init__(self): # self.task_holders = [TaskHolder() for i in range(3)] self.cap_to_process_to_queue = deque() self.process_to_display_queue = deque() self.img_processor = ImageProcessor() self.sound_player = SoundPlayer() # thread_capture = threading.Thread(target=self.capture, args=()) # thread_process = threading.Thread(target=self.process, args=()) # thread_display = threading.Thread(target=self.display, args=()) # thread_cap_disp = threading.Thread(target=self.cap_display, args=()) thread_process = threading.Thread(target=self.process, args=()) thread_process.start() self.cap_display_independent()
def collect_samples(data_type, num_samples, saveOnKey=False): s_activated = False directory = f'{ROOT_SAMPLES_DIR}/{data_type}/' os.system(f'mkdir {directory}') last_file_name = get_last_file_name(directory) cap = cv2.VideoCapture(0) drum_area = DrumArea(top_left_corner=(50, 50), square_dim=SQUARE_DIM, sound='j') #drum_area2 = DrumArea(top_left_corner=(800, 50), square_dim=SQUARE_DIM, sound='j') area_listener = AreaListener(drum_areas=[drum_area]) img_process = ImageProcessor() count = last_file_name - 1 while True: _, frame = cap.read() #frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) frame = img_process.horizontal_flip(frame) targeted_area = area_listener.get_all_target_areas(img=frame)[0] area_listener.draw_area(frame) cv2.imshow('Target', targeted_area) cv2.imshow('Main', frame) key = cv2.waitKey(1) if key == ord('s'): if not s_activated: print('S activated') s_activated = True count = last_file_name + 1 if count > last_file_name and count <= (num_samples + last_file_name): if saveOnKey and key != ord('a'): continue #file_name = get_file_name(data_type, count) file_name = 'mask_images/stick2.jpg' cv2.imwrite(file_name, targeted_area) count += 1 print(f'Saved {file_name}') elif count > num_samples + last_file_name: return if key & 0xFF == ord('q'): break
def __init__(self, ref_frames_data_filename, ref_pickle_filename, test_pickle_filename): print "init..." self.previous_obs = None self.image_processor = ImageProcessor() self.descriptors_ref = self.image_processor.load_sift(ref_pickle_filename) self.descriptors_test = self.image_processor.load_sift(test_pickle_filename) hmm = self.ref_frames_data_to_hmm(ref_frames_data_filename) #emission_probabilities = map(lambda x: complementary_normal_distribution_cdf(x,0,EMISSION_SIGMA),range(0,int(3.0*EMISSION_SIGMA))) priors=dict([(state,1.0/len(hmm)) for state in hmm]) self.viterbi = Viterbi(hmm,self.emission_probability, constraint_length=2500, # BE CAREFUL with it. walking may take long time and higher value may be needed here priors=priors)
def test_image_processor(self, source_image_storage, descriptor_search, feature_engine): source_image_storage.return_value.save_source_image.return_value = self.ref_source feature_engine.return_value.extract_features.return_value = self.list_image_regions descriptor_search.return_value.find_similar_for_region.side_effect = [cleaned_result1, cleaned_result2] source_image_storage.return_value.get_metadata_by_id.side_effect = [self.metadata] * 3 result = ImageProcessor().build_search(self.image, self.metadata, save=True) descriptor_search.assert_called_once_with(descriptor_shape=(16 * 3,), flush_data=False) descriptor_search.return_value.find_similar_for_region.assert_has_calls( [call(region) for region in self.list_image_regions]) feature_engine.assert_called_once_with(HistogramFeatureExtractor(), VerticalSplit()) feature_engine.return_value.extract_features.assert_called_once_with(self.expected_normalized) source_image_storage.assert_called_once_with(flush_data=False) source_image_storage.return_value.save_source_image.assert_called_once_with(self.expected_normalized, self.metadata) assert result == [cleaned_result1, cleaned_result2]
def config(self): """Configures the application at start-up. Controllers are responsible for initializing the application and creating all of the other objects. This method does just that. It loads the currently selected image file, creates an ImageArray for that file, creates an ImageProcessor to handle the array, and connects the ImageProcessor to the two ImagePanel objects.""" # Load the image into an array image_array = ImageArray.LoadFile(self.source) # Create the processor for the given ImageArray self.image_processor = ImageProcessor(image_array) # Set up the display panels self.original_image_panel = ImagePanel(self.original_image, self.image_processor.original) self.current_image_panel = ImagePanel(self.current_image, self.image_processor.current)
def __init__(self): rospy.init_node('state_machine') self.markers_subscriber = rospy.Subscriber(AR_TAG_TOPIC, AlvarMarkers, self.markers_callback, queue_size=10) self.starter_subscriber = rospy.Subscriber(STARTER_TOPIC, Bool, self.start_callback, queue_size=10) self.lights_off = False self.state = State.nothing self.drive = Drive() self.image_processor = ImageProcessor() self.cv_bridge = CvBridge() self.drive_speed = 1.2503309 self.left_goal_dist = 0.90 self.right_goal_dist = 0.8
def test_live(model, adaptive_threshold=GLOBAL_ADAPTIVE_THRESHOLD): # if True: # return cap = cv2.VideoCapture(0) # drum_area = DrumArea(top_left_corner=(900, 100), square_dim=RESIZE_DIM, sound='c') # drum_area2 = DrumArea(top_left_corner=(100, 100), square_dim=RESIZE_DIM, sound='j') # drum_area3 = DrumArea(top_left_corner=(100, 400), square_dim=RESIZE_DIM, sound='k') drum_area1 = DrumArea(top_left_corner=(50, 50), square_dim=320, sound='c') drum_area2 = DrumArea(top_left_corner=(850, 50), square_dim=320, sound='j') dareas = [drum_area1]#, drum_area2] area_listener2 = AreaListener(drum_areas=dareas) img_process2 = ImageProcessor() probability_model2 = tf.keras.Sequential([model, tf.keras.layers.Softmax()]) base_set = False base_imgs = None while True: _, frame_orig = cap.read() frame_orig = img_process2.horizontal_flip(frame_orig) frame_color = frame_orig.copy() #frame = cv2.cvtColor(frame_orig, cv2.COLOR_BGR2GRAY) frame = frame_color.copy() if not base_set: area_listener2.set_base_image(frame) base_imgs = area_listener2.get_base_imgs(resize_dim=(RESIZE_DIM, RESIZE_DIM)) base_set = True for drum_area in dareas: process_area_cv2Diff(drum_area, frame_orig, probability_model2) area_listener2.draw_area(frame_color) key = cv2.waitKey(1) cv2.imshow('Main', frame_color) cv2.waitKey(1) if key & 0xFF == ord('q'): break