def segment_driver(driver_id): ''' this generated the segments in settings.SEGMENTS_FOLDER[1] ''' da = DataAccess() for ride_id_minus_1, ride in enumerate(da.get_rides(driver_id)): ride_id = ride_id_minus_1 + 1 if da.skip_segment(driver_id, ride_id): continue # apply the Ramer-Douglas-Peucker algorithm ride = [p + [i] for i, p in enumerate(smoothen(ride))] # enrich with timestamp ride = rdp(ride, epsilon=10) lengths = [util.euclidian_distance(ride[i-1], ride[i]) for i in xrange(1, len(ride))] times = [ride[i][2] - ride[i-1][2] for i in xrange(1, len(ride))] angles = [util.get_angle(ride[i-2], ride[i-1], ride[i]) for i in xrange(2, len(ride))] # bucket the values lengths = util.bucket(np.log(lengths), 25, [2.2,8]) # [int(l) for l in lengths] times = util.bucket(np.log(times), 20, [1,5.5]) # [int(t) for t in times] angles = util.bucket(angles, 30, [0,180]) # [int(a) for a in angles] # write results da.write_ride_segments(driver_id, ride_id, lengths, times, angles) logging.info('finished segmenting driver %s' % driver_id)
def segment_driver(driver_id): ''' this generated the segments in settings.SEGMENTS_FOLDER[1] ''' da = DataAccess() for ride_id_minus_1, ride in enumerate(da.get_rides(driver_id)): ride_id = ride_id_minus_1 + 1 if da.skip_segment(driver_id, ride_id): continue # apply the Ramer-Douglas-Peucker algorithm ride = [p + [i] for i, p in enumerate(smoothen(ride))] # enrich with timestamp ride = rdp(ride, epsilon=10) lengths = [ util.euclidian_distance(ride[i - 1], ride[i]) for i in xrange(1, len(ride)) ] times = [ride[i][2] - ride[i - 1][2] for i in xrange(1, len(ride))] angles = [ util.get_angle(ride[i - 2], ride[i - 1], ride[i]) for i in xrange(2, len(ride)) ] # bucket the values lengths = util.bucket(np.log(lengths), 25, [2.2, 8]) # [int(l) for l in lengths] times = util.bucket(np.log(times), 20, [1, 5.5]) # [int(t) for t in times] angles = util.bucket(angles, 30, [0, 180]) # [int(a) for a in angles] # write results da.write_ride_segments(driver_id, ride_id, lengths, times, angles) logging.info('finished segmenting driver %s' % driver_id)
def get_angle(angle_window): angle_window = np.array(angle_window) normalized = angle_window.T / np.maximum(np.linalg.norm(angle_window, axis=1), 0.1) average = np.mean(normalized, axis=1) angle = util.get_angle([1,0], [0,0], average) if average[1] < 0: angle = 360 - angle return angle
def action2cmd(self, cmd): if cmd < 4: cmd = cmd return [config.ACTION_DICT_4[cmd]] else: enemy = random.sample(self.enemy_list, 1)[0] angle = util.get_angle(self.me, enemy) return [config.turn(angle), config.FIRE]
def find_columns(self): undone_prices = self.prices.copy() while len(undone_prices) > 0: price1 = undone_prices.pop(0) column = Column(price1) for price2 in undone_prices.copy(): angle = get_angle(price1.top_right, price2.top_right) if get_abs_perp_angle_diff(self.angle, angle) < self.COLUMN_ANGLE_TRESHOLD: undone_prices.remove(price2) column.add(price2) self.columns.append(column) self.columns.sort( key=lambda column_: column_.get_rotated_x(self.angle))
def find_writing_angle(self): """ take <=30 lines from the receipt and determine the angle of writing """ some_lines = self.lines if len( self.lines) <= Receipt.ANGLE_SAMPLE_SIZE else [ self.lines[i] for i in range(0, len(self.lines), len(self.lines) // Receipt.ANGLE_SAMPLE_SIZE) ] angles = [ get_angle(line.top_left, line.top_right) for line in some_lines ] self.angle = sum(angles) / len(angles) return self.angle
def segment_driver_v2(driver_id): ''' this generated the segments in settings.SEGMENTS_FOLDER[2] ''' da = DataAccess() for ride_id_minus_1, ride in enumerate(da.get_rides(driver_id)): ride_id = ride_id_minus_1 + 1 if da.skip_segment(driver_id, ride_id, version=2): continue # apply the Ramer-Douglas-Peucker algorithm ride = [p + [i] for i, p in enumerate(ride)] # enrich with timestamp ride = rdp(ride, epsilon=4) lengths = [ util.euclidian_distance(ride[i - 1], ride[i]) for i in range(1, len(ride)) ] times = [ride[i][2] - ride[i - 1][2] for i in range(1, len(ride))] angles = [ util.get_angle(ride[i - 2], ride[i - 1], ride[i]) for i in range(2, len(ride)) ] lengths = np.histogram(lengths, bins=list(range(0, 700, 20)) + [1000000000])[0] times = np.histogram(times, bins=list(range(0, 60, 4)) + [1000000000])[0] angles = np.histogram(angles, bins=list(range(0, 181, 20)))[0] # write results da.write_ride_segments(driver_id, ride_id, lengths, times, angles, version=2) logging.info('finished segmenting driver %s' % driver_id)
def convert2dec(self, n_machine, data) -> dict: """ 将原始十六进制字符串转成十进制整数,同时变成字典数据 :param n_machine: :param data: :return: """ new_data = {} for i in range(1, n_machine + 1): start = (i - 1) * 16 machine_data = [] machine_data.append(int(data[start:start + 4], 16)) machine_data.append(int(data[start + 4:start + 8], 16)) machine_data.append(int(data[start + 8:start + 12], 16)) t = data[start + 12:start + 16] angle = util.get_angle(t) / 100 angle = "{:.2f}".format(angle) # print("十六进制:", t, "角度:", angle) machine_data.append(angle) new_data[i] = machine_data return new_data
def segment_driver_v2(driver_id): ''' this generated the segments in settings.SEGMENTS_FOLDER[2] ''' da = DataAccess() for ride_id_minus_1, ride in enumerate(da.get_rides(driver_id)): ride_id = ride_id_minus_1 + 1 if da.skip_segment(driver_id, ride_id, version=2): continue # apply the Ramer-Douglas-Peucker algorithm ride = [p + [i] for i, p in enumerate(ride)] # enrich with timestamp ride = rdp(ride, epsilon=4) lengths = [util.euclidian_distance(ride[i-1], ride[i]) for i in xrange(1, len(ride))] times = [ride[i][2] - ride[i-1][2] for i in xrange(1, len(ride))] angles = [util.get_angle(ride[i-2], ride[i-1], ride[i]) for i in xrange(2, len(ride))] lengths = np.histogram(lengths, bins=range(0, 700, 20) + [1000000000])[0] times = np.histogram(times, bins=range(0, 60, 4) + [1000000000])[0] angles = np.histogram(angles, bins=range(0, 181, 20))[0] # write results da.write_ride_segments(driver_id, ride_id, lengths, times, angles, version=2) logging.info('finished segmenting driver %s' % driver_id)
def main(): ''' main function''' img_dir = "img_dir/" # image file dir # setup process and queues for multiprocessing. img_queue = Queue() box_queue = Queue() img_list = [] for filename in os.listdir(img_dir): frame = cv2.imread(img_dir + filename) img_list.append(frame) img_queue.put(frame) box_process = Process(target=get_face, args=(img_queue, box_queue)) box_process.start() # introduce mark_detector to detect landmarks mark_detector = MarkDetector() while True: # Crop it if frame is larger than expected. # frame = frame[0:480, 300:940] # flipcode > 0: horizontal flip; flipcode = 0: vertical flip; flipcode < 0: horizental and vertical flip # frame = cv2.flip(frame, 2) if len(img_list) != 0: frame = img_list.pop(0) raw_frame = frame.copy() # introduce pos estimator to solve pose. Get one frames to setup the # estimator according to the images size. height, width = frame.shape[:2] pose_estimator = PoseEstimator(img_size=(height, width)) else: break # Pose estimation by 3 steps. # 1. detect faces. # 2. detect landmarks. # 3. estimate pose. # get face from box queue. faceboxes = box_queue.get() # print("the length of facebox is " + str(len(faceboxes))) for facebox in faceboxes: # detect landmarks from image 128 * 128 face_img = frame[facebox[1]: facebox[3], facebox[0]: facebox[2]] # cut off the area of face. face_img = cv2.resize(face_img, (CNN_INPUT_SIZE, CNN_INPUT_SIZE)) face_img = cv2.cvtColor(face_img, cv2.COLOR_BGR2RGB) # BGR -> RGB marks = mark_detector.detect_marks(face_img) # covert the marks locations from local CNN to global image. marks[:, 0] *= (facebox[2] - facebox[0]) # the width of rectangle. marks[:, 1] *= (facebox[3] - facebox[1]) # the length of rectangle. marks[:, 0] += facebox[0] marks[:, 1] += facebox[1] # uncomment following line to show raw marks. # util.draw_marks(frame, marks, color=(0, 255, 0)) # util.draw_faceboxes(frame, facebox) # try pose estimation with 68 points. pose = pose_estimator.solve_pose_by_68_points(marks) # pose[0] is rotation_vector, and pose[1] is translation_vector pose_np = np.array(pose).flatten() pose = np.reshape(pose_np, (-1, 3)) angle = util.get_angle(pose[0]) pose_estimator.get_warp_affined_image(frame, facebox, marks, angle[2]) # show preview cv2.imshow("Preview", frame) cv2.imshow("raw_frame", raw_frame) if cv2.waitKey(0) == 27: continue # clean up the multiprocessing process. box_process.terminate() box_process.join()