def predict(self, path_image): img_ = themvien(path_image, (TARGET_SIZE, TARGET_SIZE), 0) emb = incpetion_predict(img_) lab_img = cv2.cvtColor(img_, cv2.COLOR_BGR2LAB) l_channel, a_channel, b_channel = cv2.split(lab_img) img_resize = cv2.resize(img_, (RESIZE_SIZE, RESIZE_SIZE)) img_resize = np.array(img_resize, dtype = float) x = rgb2lab(1.0/255*img_resize)[:, :, 0] x = x.reshape(1, RESIZE_SIZE, RESIZE_SIZE, 1) output = self.model_.predict([x, emb]) output *= 128 cur = np.zeros((RESIZE_SIZE, RESIZE_SIZE, 3)) cur[:, :, 0] = x[0][:, :, 0] cur[:, :, 1:] = output[0] result_name = str(uuid.uuid4()) + '.png' imsave('./static/image/' + result_name, lab2rgb(cur)) l, a, b = processing('./static/image/' + result_name) out = np.zeros((TARGET_SIZE, TARGET_SIZE, 3), dtype = np.uint8) out[:, :, 0] = l_channel out[:, :, 1] = a out[:, :, 2] = b out = cv2.cvtColor(out, cv2.COLOR_LAB2BGR) cv2.imwrite('./static/image/' + result_name, out) return result_name
def init(files, folder=''): data = [] t_sizes = [] for file in files: _, edges_parameter, lines_parameter = read_params_file(file, folder=folder) target = imageProcessing.processing(file, folder=folder, edges_parameter=edges_parameter, lines_parameter=lines_parameter) data.append((file, target)) t_sizes.append(target.size) return max(t_sizes), data
def routine(frame, camera_mks_rm): global rel_x_ratio, num_data processed_frame, explain1 = processing(frame) choosen_left, choosen_right, explain2 = sliding_window(processed_frame) ret_left, linear_func_left = get_sliding_window_function(choosen_left) ret_right, linear_func_right = get_sliding_window_function( choosen_right) if ret_left and ret_right: steering_angle, explain2 = get_steering_angle_from_linear_function( (linear_func_left + linear_func_right) / 2, explain2) # left = linear_func_left(480) # right = linear_func_right(480) # print(left, right, right - left) # steering_angle = filter_deg.get_moving_average(steering_angle) elif ret_left: steering_angle, explain2 = get_steering_angle_from_linear_function( linear_func_left, explain2, rel_x_ratio=1.2) # steering_angle = filter_deg.get_moving_average(steering_angle) elif ret_right: steering_angle, explain2 = get_steering_angle_from_linear_function( linear_func_right, explain2, rel_x_ratio=0.8) # steering_angle = filter_deg.get_moving_average(steering_angle) else: steering_angle = filter_deg2.prev_lpf # steering_angle = filter_deg.prev_avg steering_angle = filter_deg2.get_lpf(steering_angle)[0] _, explain2 = tfcp.get_segment(explain2, camera_mks_rm, linear_func_left, linear_func_right) # ret_line, _ = tfcp.get_line(explain2, num_data, camera_mks_rm) # Merge Explain vertical_line = np.zeros((explain1.shape[0], 5, 3), dtype=np.uint8) explain_merge = np.hstack((explain2, vertical_line, explain1)) return steering_angle, explain_merge
def routine(frame): global rel_x_ratio processed_frame, explain1 = processing(frame) choosen_left, choosen_right, explain2 = sliding_window(processed_frame) ret_left, linear_func_left = get_sliding_window_function(choosen_left) ret_right, linear_func_right = get_sliding_window_function(choosen_right) if ret_left and ret_right: steering_angle, explain2 = get_steering_angle_from_linear_function((linear_func_left + linear_func_right)/2, explain2) # left = linear_func_left(480) # right = linear_func_right(480) # print(left, right, right - left) ## distance ~= 435.662 PIXEL or 0.845m ## 515.58 PIXEL / m ## Offset from lidar to ROI : 0.400 m # steering_angle = filter_deg.get_moving_average(steering_angle) elif ret_left: steering_angle, explain2 = get_steering_angle_from_linear_function(linear_func_left, explain2, rel_x_ratio=1.2) # steering_angle = filter_deg.get_moving_average(steering_angle) elif ret_right: steering_angle, explain2 = get_steering_angle_from_linear_function(linear_func_right, explain2, rel_x_ratio=0.8) # steering_angle = filter_deg.get_moving_average(steering_angle) else: steering_angle = filter_deg2.prev_lpf # steering_angle = filter_deg.prev_avg steering_angle = filter_deg2.get_lpf(steering_angle)[0] # Merge Explain vertical_line = np.zeros((explain1.shape[0], 5, 3), dtype=np.uint8) explain_merge = np.hstack((explain2, vertical_line, explain1)) return steering_angle, explain_merge
def start(self): class_name = 'QWidget' title_name = '金山打字通 2016' self.hwnd = gui.FindWindow(class_name,title_name) #print(self.hwnd) if self.hwnd: #gui.CloseWindow(self.hwnd) left, top, right, bottom = self.gui_control() if (left<0 or top<0 or right<0 or bottom<0): raise Exception("Please don't hide the window!") left += 45 top += 155 right -= 50 bottom -= 130 rect = (left, top, right, bottom) img = ImageGrab.grab().crop(rect) ''' plt.imshow(img) plt.show() ''' if self.isSave: for j in range(5): for i in range(66): img_ready = img.crop((10.4*i,63.8*j,10.4*(i+1),63.8*j+18)) plt.imshow(img_ready) plt.xticks([]), plt.yticks([]) plt.show() img_ready.save(PICTURE_PATH.format(self.title, j, i)) char = input("Please enter the char: ") with open(LABEL_PATH,'a') as f: f.write("./pictures/{}.{}.{}.jpg {}\n".format(self.title, j, i, ord(char))) elif self.isGray: for j in range(5): for i in range(66): img_croped = img.crop((10.4*i,63.8*j,10.4*(i+1),63.8*j+18)) ''' plt.imshow(img_croped) plt.xticks([]), plt.yticks([]) plt.show() ''' img_processed = image_processing.processing(img_croped,False) img_ready = pre_pic.pre_pic(img_processed,isApp=True) if j==0 and i==0: imgs = img_ready else: imgs = np.r_[imgs,img_ready] #print(imgs) yield self.hwnd,imgs else: for j in range(5): for i in range(66): img_croped = img.crop((10.4*i,63.8*j,10.4*(i+1),63.8*j+18)) ''' plt.imshow(img_croped) plt.xticks([]), plt.yticks([]) plt.show() ''' img_processed = image_processing.processing(img_croped,False) img_ready = pre_pic.pre_pic(img_processed,isApp=True,isGray=False) if j==0 and i==0: imgs = img_ready else: imgs = np.r_[imgs,img_ready] #print(imgs) yield self.hwnd,imgs else: raise Exception("金山打字通2016 窗口捕获失败!")
minVal = 15_000 maxVal = 20_000 aperture_Size = 7 L2_gradient = True rho = 7 threshold = 2 min_line_length = 2 max_line_gap = 4 # parameters for Target detection edges_parameter = imageProcessing.edges_parm(minVal, maxVal, aperture_Size, L2_gradient) lines_parameter = imageProcessing.lines_parm(rho, threshold, min_line_length, max_line_gap) target = imageProcessing.processing(file, folder, edges_parameter, lines_parameter) print('IND_SIZE: ' + str(target.size)) print('Height: ' + str(target.height)) print('Width: '+ str(target.width)) viewer.show_individual(target) viewer.save_image(viewer.draw_individual(target), name='tmp/' + file + '_size_' +str(target.size)) # make params-file for input image data = [] data.append(file) data.append('edges_parameter') data.append('minVal= ' + str(minVal)) data.append('maxVal= ' + str(maxVal)) data.append('aperture_Size= ' + str(aperture_Size))
def routine(frame, POS): global filter_coef_left, filter_coef_right, heading_deg frame = calibrate_image(frame) processed_frame, explain1 = processing(frame) processed_height, processed_width = processed_frame.shape[:2] processed_height2 = processed_height // 3 * 2 choosen_left, choosen_right, explain2 = sliding_window(processed_frame) func_coef_left = get_linear_function(choosen_left) func_coef_right = get_linear_function(choosen_right) if func_coef_left and func_coef_right: for coef in [func_coef_left, func_coef_right]: func = np.poly1d(coef) new_ys = np.linspace(0, processed_frame.shape[0], num=processed_frame.shape[0], endpoint=True) new_xs = func(new_ys) for x, y in zip(new_xs, new_ys): if 0 < y < processed_frame.shape[ 0] and 0 < x < processed_frame.shape[1]: x, y = int(x), int(y) cv.circle(explain2, (x, y), 3, (0, 255, 255), -1) for coef in [(func_coef_left + func_coef_right) / 2]: func = np.poly1d(coef) new_ys = np.linspace(0, processed_frame.shape[0], num=processed_frame.shape[0], endpoint=True) new_xs = func(new_ys) for x, y in zip(new_xs, new_ys): if 0 < y < processed_frame.shape[ 0] and 0 < x < processed_frame.shape[1]: x, y = int(x), int(y) cv.circle(explain2, (x, y), 3, (0, 0, 255), -1) heading_src = (processed_width // 2, processed_height) heading_dst = (func(processed_height2), processed_height2) heading_sub = (heading_src[0] - heading_dst[0], heading_src[1] - heading_dst[1]) heading_rad = np.arctan2(heading_sub[1], heading_sub[0]) heading_deg = np.degrees(heading_rad) # heading_deg = filter_deg.LPF(np.array([heading_deg]))[0] heading_deg = filter_deg2.get_lpf(heading_deg) steering_deg = heading_deg - 90 heading_rad = np.radians(heading_deg) gradient = np.tan(heading_rad) heading_dst = ((processed_height2 - processed_height) / gradient + processed_width // 2, processed_height2) heading_src = tuple(rint(heading_src)) heading_dst = tuple(rint(heading_dst)) cv.circle(explain2, heading_src, 3, (0, 0, 255), -1) cv.circle(explain2, heading_dst, 3, (0, 0, 255), -1) cv.line(explain2, heading_src, heading_dst, (0, 0, 255), 1) else: # heading_rad = np.radians(heading_deg) # gradient = np.tan(heading_rad) + 1e-10 # heading_src = (processed_width//2, processed_height) # heading_dst = ((processed_height2 - heading_src[1])/gradient + heading_src[0], processed_height2) # heading_src = tuple(rint(heading_src)) # heading_dst = tuple(rint(heading_dst)) # cv.circle(explain2, heading_src, 3, (0, 0, 255), -1) # cv.circle(explain2, heading_dst, 3, (0, 0, 255), -1) # cv.line(explain2, heading_src, heading_dst, (0, 0, 255), 1) if func_coef_left: func = np.poly1d(func_coef_left) new_ys = np.linspace(0, processed_frame.shape[0], num=processed_frame.shape[0], endpoint=True) new_xs = func(new_ys) left_src = (func(processed_height), processed_height) left_dst = (func(processed_height2), processed_height2) left_sub = (left_src[0] - left_dst[0], left_src[1] - left_dst[1]) left_rad = np.arctan2(left_sub[1], left_sub[0]) heading_deg = np.degrees(left_rad) heading_deg = filter_deg2.get_lpf(heading_deg) steering_deg = heading_deg - 90 heading_rad = np.radians(heading_deg) gradient = np.tan(heading_rad) + 1e-10 heading_src = (processed_width // 2, processed_height) heading_dst = ( (processed_height2 - processed_height) / gradient + processed_width // 2, processed_height2) heading_src = tuple(rint(heading_src)) heading_dst = tuple(rint(heading_dst)) cv.circle(explain2, heading_src, 3, (0, 0, 255), -1) cv.circle(explain2, heading_dst, 3, (0, 0, 255), -1) cv.line(explain2, heading_src, heading_dst, (0, 0, 255), 1) elif func_coef_right: func = np.poly1d(func_coef_right) new_ys = np.linspace(0, processed_frame.shape[0], num=processed_frame.shape[0], endpoint=True) new_xs = func(new_ys) right_src = (func(processed_height), processed_height) right_dst = (func(processed_height2), processed_height2) right_sub = (right_src[0] - right_dst[0], right_src[1] - right_dst[1]) right_rad = np.arctan2(right_sub[1], right_sub[0]) heading_deg = np.degrees(right_rad) heading_deg = filter_deg2.get_lpf(heading_deg) steering_deg = heading_deg - 90 heading_rad = np.radians(heading_deg) gradient = np.tan(heading_rad) + 1e-10 heading_src = (processed_width // 2, processed_height) heading_dst = ( (processed_height2 - processed_height) / gradient + processed_width // 2, processed_height2) heading_src = tuple(rint(heading_src)) heading_dst = tuple(rint(heading_dst)) cv.circle(explain2, heading_src, 3, (0, 0, 255), -1) cv.circle(explain2, heading_dst, 3, (0, 0, 255), -1) cv.line(explain2, heading_src, heading_dst, (0, 0, 255), 1) else: new_ys = [] new_xs = [] for x, y in zip(new_xs, new_ys): if 0 < y < processed_frame.shape[ 0] and 0 < x < processed_frame.shape[1]: x, y = int(x), int(y) cv.circle(explain2, (x, y), 3, (255, 255, 0), -1) print(heading_deg) # Merge Explain vertical_line = np.zeros((explain1.shape[0], 5, 3), dtype=np.uint8) explain_merge = np.hstack((explain2, vertical_line, explain1)) cv.putText(explain_merge, "POS: {}".format(int(POS)), (0, 40), cv.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2) return explain_merge
image = image.reshape(784, ) image = np.expand_dims( image, axis=0) # p转换成了2维,模型第一层定义了输入格式为2维: input_shape=(784,) # 测试图片 pre = model1.predict(image) return np.argmax(pre, axis=1)[0] def recognition2(image): """ 使用模型2识别 :param image: 待识别图片 :return: 识别结果 """ # 转化为列表的array格式 image_list = [] image = get_feature(image) image_list.append(image) image_array = np.array(image_list) # 测试图片 pre = model2.predict(image_array) return np.argmax(pre, axis=1)[0] if __name__ == '__main__': p, o_image = processing("./image/1.png") recognition1(o_image) recognition2(o_image)