def __init__(self): super().__init__() self.setupUi(self) self.dlt = DLT() self.statusbar.showMessage('正在更新数据库……') last_term, new_last_term = self.dlt.update() self.statusbar.showMessage(f'数据库大乐透表原有到{last_term}期,现更新到{new_last_term}期') self.dlt.get_history(terms=50) self.hitory_table() self.frq_bars()
def __init__(self, path): self.path = path self.d_function = DLT(224) self.search_index = 0 self.scale = 1 self.trans = [0, 0] self.current_ground_truth = None self.frame_num = 0 self.initialize() self.IOU = 0
def generate_single(self, folder_id, sub_dir): print("Processing " + sub_dir) entries = os.listdir(sub_dir) entries.sort() d = DLT(self.bounding_size) folder_name = sub_dir.split('/')[-1] try: os.mkdir(self.target_directory + 'scale_' + folder_name + '/') except: pass with open(self.target_directory + 'scale_' + folder_name + '/scale_' + folder_name + '.txt', 'w', encoding="UTF-8") as target: pass txt_file = sub_dir + '.txt' with open(txt_file, 'r', encoding="UTF-8") as source: lines = source.readlines() try: os.mkdir(self.target_directory + 'scale_' + folder_name + '/template_scale_' + folder_name) except: pass for i in range(len(lines[1:])): line_list = lines[i + 1].split() img = cv2.imread(sub_dir + '/' + line_list[0]) line_list = [float(x) for x in line_list[1:]] H_template, template_window = d.get_template_window( img, line_list) cv2.imwrite( self.target_directory + 'scale_' + folder_name + '/template_scale_' + folder_name + '/template' + str(i + 1) + '.jpg', template_window) lines = lines[1:] try: os.mkdir(self.target_directory + 'scale_' + folder_name + '/search_window_scale_' + folder_name) except: pass for i in range(1, len(lines)): line_list_up = lines[i - 1].split() line_list_down = lines[i].split() img2 = cv2.imread(sub_dir + '/' + line_list_down[0]) if img2 is None: temp = line_list_down[i].split('e') img2 = cv2.imread(sub_dir + '/' + temp[0] + 'e0' + temp[1]) line_list_up = line_list_up[1:] line_list_up = [float(x) for x in line_list_up] line_list_down = line_list_down[1:] line_list_down = [float(x) for x in line_list_down] self.scale = np.random.uniform(self.scale_range[0], self.scale_range[1], 1)[0] H_search, search_window, four_points = d.get_search_window( img2, line_list_up, line_list_down, self.scale, self.trans) W = d.DLT_transfer([ 0, 0, d.bounding_size - 1, 0, d.bounding_size - 1, d.bounding_size - 1, 0, d.bounding_size - 1 ], [ four_points[0][0], four_points[0][1], four_points[1][0], four_points[1][1], four_points[2][0], four_points[2][1], four_points[3][0], four_points[3][1] ]) cv2.imwrite( self.target_directory + 'scale_' + folder_name + '/search_window_scale_' + folder_name + '/' + str(i) + '.jpg', search_window) with open(self.target_directory + 'scale_' + folder_name + '/scale_' + folder_name + '.txt', 'a', encoding="UTF-8") as target: target.write(str(i) + '.jpg') W_inv = np.linalg.inv(W) W_inv = W_inv / W_inv[2][2] W = np.resize(W, (9, )) W_inv = np.resize(W_inv, (9, )) for i in range(8): target.write(' ' + str(round(W[i], ndigits=6))) for i in range(8): target.write(' ' + str(round(W_inv[i], ndigits=6))) target.write("\n")
class UI(QtWidgets.QMainWindow, Ui_GUI_PyQT5): def __init__(self): super().__init__() self.setupUi(self) self.dlt = DLT() self.statusbar.showMessage('正在更新数据库……') last_term, new_last_term = self.dlt.update() self.statusbar.showMessage(f'数据库大乐透表原有到{last_term}期,现更新到{new_last_term}期') self.dlt.get_history(terms=50) self.hitory_table() self.frq_bars() def frq_bars(self): """ :return: """ freq = self.dlt.get_freq() dpi = 100 canvas = FigureCanvas(Figure(figsize=((self.graphicsView.width()-2)/dpi, (self.graphicsView.height()-2)/dpi))) # axes = canvas.figure.subplots(2, 1) # ax_front = axes[1] # ax_back = axes[0] # ax_front = canvas.figure.subplots() ax_front = canvas.figure.add_axes([0.1, 0, 0.9, 1]) # position = ax_front.get_position() # bbox = position #[*position[0], *[x-y for x, y in zip(*position)]] # ax_back = canvas.figure.add_axes(bbox) ax_back = canvas.figure.add_axes([0, 0, 0.9, 1]) ax_front.xaxis.set_visible(False) ax_back.xaxis.set_visible(False) ax_back.invert_xaxis() ax_back.yaxis.set_ticks_position('right') ax_back.patch.set_alpha(0) # ax_back.patch.set_facecolor('w') ax_front_data = np.sort(freq['front'].values[0]) ax_front_labels = [self.dlt.front_balls[x] for x in np.argsort(freq['front'].values[0])] ax_back_data = np.sort(freq['back'].values[0])[::-1] ax_back_labels = [self.dlt.back_balls[x] for x in np.argsort(freq['back'].values[0])][::-1] rects_front = ax_front.barh(ax_front_labels, ax_front_data, color=(1, 1, 0, 1)) rects_back = ax_back.barh(ax_back_labels, ax_back_data, color=(0, 1, 0, 1)) rect_length = 1 # ax_front_data.max() + ax_back_data.max() ax_back.set_xlim([rect_length, 0]) ax_front.set_xlim([0, rect_length]) for rect in rects_front: width = rect.get_width() ax_front.annotate(f'{width:.2f}', xy=(width, rect.get_y() + rect.get_height() / 2), xytext=(3, 0), textcoords="offset points", ha='left', va='center_baseline') for rect in rects_back: width = rect.get_width() ax_back.annotate(f'{width:.2f}', xy=(width, rect.get_y() + rect.get_height() / 2), xytext=(-3, 0), # 3 points vertical offset textcoords="offset points", ha='right', va='center') graphic_scene = QtWidgets.QGraphicsScene() graphic_scene.addWidget(canvas) self.graphicsView.setScene(graphic_scene) self.graphicsView.show() def hitory_table(self): """ :return: """ self.tableView.setModel(DataFrameModel(self.dlt.history)) self.tableView.resizeColumnsToContents() # 自动调整列宽 def refresh(self): """ :return: """ recent = int(self.lineEdit_recent.text()) self.dlt.get_history(terms=recent) self.frq_bars() self.hitory_table()
class data_stuff(object): def __init__(self, path): self.path = path self.d_function = DLT(224) self.search_index = 0 self.scale = 1 self.trans = [0, 0] self.current_ground_truth = None self.frame_num = 0 self.initialize() self.IOU = 0 # self.max_length = 0 def initialize(self): with open(self.path + '.txt', 'r', encoding="UTF-8") as source: lines = source.readlines() line_list = lines[1].split() self.frame_num = len(lines) - 3 img = cv2.imread(self.path + '/' + line_list[0]) line_list = line_list[1:] self.max_length = len(lines) line_list = [float(x) for x in line_list] M, dst = self.d_function.get_template_window(img, line_list) line_list_down = lines[2].split() img2 = cv2.imread(self.path + '/' + line_list_down[0]) line_list_down = line_list_down[1:] line_list_down = [float(x) for x in line_list_down] H_search, search_window, four_points = self.d_function.get_search_window( img2, line_list, line_list_down, self.scale, self.trans) self.four_points = four_points self.warp_matrix = H_search self.template_windows = dst self.search_window = search_window self.img = img2 self.current_ground_truth = line_list_down # return dst, search_window def get_template(self): return self.template_windows def get_image(self, absolute_coor): # print(self.search_index) if self.search_index == 0: self.search_index += 1 return self.search_window, self.img with open(self.path + '.txt', 'r', encoding="UTF-8") as source: lines = source.readlines() lines = lines[1:] line_list = lines[self.search_index + 1].split() img = cv2.imread(self.path + '/' + line_list[0]) H_search, search_window = self.d_function.get_next_window( img, absolute_coor, self.scale, self.trans) line_list = [float(x) for x in line_list[1:]] # # _, _, self.four_points = self.d_function.get_search_window(img, [absolute_coor[0][0], absolute_coor[0][1], # absolute_coor[1][0], absolute_coor[1][1], # absolute_coor[2][0], absolute_coor[2][1], # absolute_coor[3][0], absolute_coor[3][1]] # ,line_list, self.scale, self.trans) self.current_ground_truth = line_list self.search_index += 1 self.warp_matrix = H_search return search_window, img def set_scale(self, scale): self.scale = scale def get_absolute_coor(self, four_points): absolute_coor = self.d_function.get_abs_coor(self.warp_matrix, four_points) self.IOU += self.calculate_IOU(absolute_coor) return absolute_coor def is_done(self): if self.search_index == self.max_length - 3: return True else: return False def calculate_IOU(self, absolute_coor): p1 = Polygon([(absolute_coor[0][0], absolute_coor[0][1]), (absolute_coor[1][0], absolute_coor[1][1]), (absolute_coor[2][0], absolute_coor[2][1]), (absolute_coor[3][0], absolute_coor[3][1])]) p2 = Polygon([ (self.current_ground_truth[0], self.current_ground_truth[1]), (self.current_ground_truth[2], self.current_ground_truth[3]), (self.current_ground_truth[4], self.current_ground_truth[5]), (self.current_ground_truth[6], self.current_ground_truth[7]) ]) p3 = p1.intersection(p2) # print(p3.area) # print(p3.area / (p1.area + p2.area - p3.area)) return p3.area / (p1.area + p2.area - p3.area) def get_IOU(self): self.IOU /= self.frame_num return self.IOU
from file2array import file2array from dlt import DLT _3dMat = file2array('3d.txt', sp=4) _imgMat = file2array('image.txt', sp=3) Matrix, err = DLT(xyz_in=_3dMat, uv_in=_imgMat)\ print('Matrix') print(Matrix) print('\nError') print(err)