Ejemplo n.º 1
0
def pic_locate(pic_match,pic_origin,thresh,findall=True,rgb_bool=True):  #pic_match is the dir path, pic_origin is the data array
    """

    :param pic_match:  源图像路径或图像数组
    :param pic_origin:  背景图像,ndarray
    :param thresh:      阈值,数值
    :param findall:     true 为寻找全部匹配图像,false为只返回一个
    :param rgb_bool:    true为匹配颜色,false为不匹配颜色
    :return:
    """
    if(isinstance(pic_match,str)):      #若为路径,则根据当前分辨率动态调整实际对比图像
        pic_test = Image.open(pic_match,'r')
        resolution = globalvar.get_window_resolution()
        max_resolution = globalvar.get_max_resolution()
        width = int(resolution[0] / max_resolution[0] * pic_test.size[0])
        height = int(resolution[1] / max_resolution[1] * pic_test.size[1])
        pic_test = np.array(pic_test.resize((width, height), Image.ANTIALIAS))
    elif(isinstance(pic_match,np.ndarray)):
        pic_test = pic_match
    if findall:
        position = aircv.find_all_template(pic_origin,pic_test,thresh,rgb=rgb_bool)
    else:
        position = aircv.find_template(pic_origin, pic_test, thresh,rgb=rgb_bool)
    # C = np.fft.ifft2(np.fft.fft2(pic_origin)*fftpack.fft2(pic_match_path,(888,1435,3)))
    return position
Ejemplo n.º 2
0
def pic_load_ram():
    #must run after getting handle, when dealing with the pics which resolutions lower than the current one,
    # the function should remove these pics which may cause unpredictable errors
    window_resolution = globalvar.get_window_resolution()
    max_resolution = globalvar.get_max_resolution()
    if window_resolution[0] == 0:
        raise Exception("未检测到模拟器")
    for keys, pic_path in pic_confirm.items():
        temp_im = pic_resize(pic_path, window_resolution,
                             pic_confirm_res[keys])
        pic_confirm[keys] = np.array(temp_im)
    for keys, pic_path in huodong_pic.items():
        temp_im = pic_resize(pic_path, window_resolution,
                             huodong_pic_res[keys])
        huodong_pic[keys] = np.array(temp_im)

    for keys, pic_path in pic_where.items():
        temp_im = pic_resize(pic_path, window_resolution, pic_where_res[keys])
        pic_where[keys] = np.array(temp_im)

    for keys, pic_path in staff_pic.items():
        temp_im = pic_resize(pic_path, window_resolution, staff_pic_res[keys])
        staff_pic[keys] = np.array(temp_im)

    for keys, pic_path in guanqia_pic.items():
        temp_im = pic_resize(pic_path, window_resolution,
                             guanqia_pic_res[keys])
        guanqia_pic[keys] = np.array(temp_im)

    #常量点 自定义分辨率适应
    for keys, values in points.items():
        if isinstance(values[0], int):
            width = int(window_resolution[0] / max_resolution[0] * values[0])
            height = int(window_resolution[1] / max_resolution[1] * values[1])
            points[keys] = [width, height]
        else:
            for index, value_temp in enumerate(values):
                width = int(window_resolution[0] / max_resolution[0] *
                            value_temp[0])
                height = int(window_resolution[1] / max_resolution[1] *
                             value_temp[1])
                values[index] = [width, height]
            points[keys] = values
Ejemplo n.º 3
0
 def get_score(self,event):
     # if self.handle==None:
     #     self.handle = get_handle()
     # im = prtsc(self.handle)
     if isinstance(self.origin_img,np.ndarray) and isinstance(self.cut_img,np.ndarray):
         pass
     else:
         wx.MessageBox("请先载入并裁剪相应图像")
         return
     im = self.origin_img
     #changed to BGR aligned to the cv2
     #im = im [:,:,::-1]
     window_resolution = globalvar.get_window_resolution()
     max_resolution = self.origin_res
     match_im = config_ark.pic_resize(self.cut_img,window_resolution,max_resolution)
     results = pic_locate(match_im,im,0.8,True,True)
     if results:
         for i in results:
             pos = i['rectangle'][0]
             width = i['rectangle'][2][0] - i['rectangle'][0][0]
             height = i['rectangle'][1][1] - i['rectangle'][0][1]
     # Draw the bounding rectangle
             #self.rect = Rectangle((0, 0), 0, 0, facecolor='None', edgecolor='red')
             #self.axes.add_patch(self.rect)
             tmp_rect = Rectangle((0, 0), 0, 0, facecolor='None', edgecolor='red')
             tmp_rect.set_width(width)
             tmp_rect.set_height(height)
             tmp_rect.set_xy(pos)
             self.axes.add_patch(tmp_rect)
             #tmp_rect.remove()
             self.draws.append(tmp_rect)
             #self.text = plt.text(pos[0],pos[1],"{0:.4f}".format(i['confidence']))
             tmp_text = self.axes.text(pos[0],pos[1],"{0:.3f}".format(i['confidence']),fontdict={'color':'red','size':12},
                                       bbox=dict(facecolor='white', alpha=0.8))
             #tmp_text.remove()
             self.draws.append(tmp_text)
         self.canvas.draw()
         for j in self.draws:
             j.remove()
         self.draws = []
     else:
         wx.MessageBox("模拟器中没有包含所选图像")
Ejemplo n.º 4
0
    def get_score(self, event):
        # if self.handle==None:
        #     self.handle = get_handle()
        # im = prtsc(self.handle)
        if isinstance(self.origin_img, np.ndarray) and isinstance(
                self.cut_img, np.ndarray):
            pass
        else:
            wx.MessageBox("请先载入并裁剪相应图像")
            return
        im = self.origin_img
        #changed to BGR aligned to the cv2
        #im = im [:,:,::-1]
        window_resolution = globalvar.get_window_resolution()
        max_resolution = self.origin_res
        match_im = config_ark.pic_resize(self.cut_img, window_resolution,
                                         max_resolution)
        # str_results = pytesseract.image_to_string(match_im,lang='eng')
        # point1, point2, point3, point4 = config_ark.baitan_price
        # im_crop = im[point2 - config_ark.ymjh_pc_shift[1]:point4 - config_ark.ymjh_pc_shift[1], point1 - config_ark.ymjh_pc_shift[0]:point3 - config_ark.ymjh_pc_shift[0], :]
        # thresh = [[220,255], [220,255], [220,255]]  # RGB
        # im_gray = cv2.cvtColor(im_crop.copy(), cv2.COLOR_BGR2GRAY)
        # im_thresh = threshhold(im_crop, thresh)
        # im_thresh = cv2.cvtColor(im_thresh.copy(), cv2.COLOR_BGR2GRAY)
        # kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (2, 2))
        # # im_dilated = cv2.dilate(im_thresh, (3, 3))
        # im_erode = cv2.erode(im_thresh, kernel)
        # # cv2.imshow("123", im_crop)
        # # cv2.waitKey()
        # results = cfs(im_erode)
        # value_result = 0
        # for rect in results:
        #     roi1 = im_gray[:, rect[0]:rect[1]]
        #     # roi = np.transpose(roi,(1,0))
        #     roi = fillout(roi1)
        #     if isinstance(roi, np.ndarray) == False:
        #         continue
        #     roi_hog_fd = hog(roi.copy(), orientations=9, pixels_per_cell=(14, 14), cells_per_block=(1, 1),
        #                      visualise=False)
        #     roi_hog_fd = self.pp.transform(np.array([roi_hog_fd], 'float64'))
        #     nbr = self.clf.predict(roi_hog_fd)
        #     value_result = value_result * 10
        #     value_result += int(nbr[0])
        #     save_digits(roi,int(nbr[0]))
        #     # cv2.imshow("123",roi1)
        #     # cv2.waitKey()
        # print(value_result)

        point1, point2, point3, point4 = config_ark.yinliang_pos
        im_crop = im[point2 - config_ark.ymjh_pc_shift[1]:point4 -
                     config_ark.ymjh_pc_shift[1],
                     point1 - config_ark.ymjh_pc_shift[0]:point3 -
                     config_ark.ymjh_pc_shift[0], :]
        thresh = [[120, 200], [120, 200], [120, 200]]  # RGB
        im_gray = cv2.cvtColor(im_crop.copy(), cv2.COLOR_BGR2GRAY)
        im_thresh = threshhold(im_crop.copy(), thresh)
        im_thresh = cv2.cvtColor(im_thresh.copy(), cv2.COLOR_BGR2GRAY)
        kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (2, 2))
        # im_dilated = cv2.dilate(im_thresh, (3, 3))
        im_erode = cv2.erode(im_thresh, kernel)
        cv2.imshow("123", im_thresh)
        cv2.waitKey()
        results = cfs(im_erode)
        value_result = 0
        for rect in results:
            roi1 = im_gray[rect[2]:rect[3], rect[0]:rect[1]]
            # roi = np.transpose(roi,(1,0))
            roi = fillout(roi1)
            if isinstance(roi, np.ndarray) == False:
                continue
            roi_hog_fd = hog(roi.copy(),
                             orientations=9,
                             pixels_per_cell=(14, 14),
                             cells_per_block=(1, 1),
                             visualise=False)
            roi_hog_fd = self.pp.transform(np.array([roi_hog_fd], 'float64'))
            nbr = self.clf.predict(roi_hog_fd)
            value_result = value_result * 10
            value_result += int(nbr[0])
            save_digits(roi, int(nbr[0]))
            # cv2.imshow("123",roi1)
            # cv2.waitKey()
        print("当前银两{}".format(value_result))

        self.scoreText8.SetLabel("{}".format(value_result))
        results = pic_locate(match_im, im, 0.8, True, True)
        if results:
            for i in results:
                pos = i['rectangle'][0]
                width = i['rectangle'][2][0] - i['rectangle'][0][0]
                height = i['rectangle'][1][1] - i['rectangle'][0][1]
                # Draw the bounding rectangle
                #self.rect = Rectangle((0, 0), 0, 0, facecolor='None', edgecolor='red')
                #self.axes.add_patch(self.rect)
                tmp_rect = Rectangle((0, 0),
                                     0,
                                     0,
                                     facecolor='None',
                                     edgecolor='red')
                tmp_rect.set_width(width)
                tmp_rect.set_height(height)
                tmp_rect.set_xy(pos)
                self.axes.add_patch(tmp_rect)
                #tmp_rect.remove()
                self.draws.append(tmp_rect)
                #self.text = plt.text(pos[0],pos[1],"{0:.4f}".format(i['confidence']))
                tmp_text = self.axes.text(pos[0],
                                          pos[1],
                                          "{0:.3f}".format(i['confidence']),
                                          fontdict={
                                              'color': 'red',
                                              'size': 12
                                          },
                                          bbox=dict(facecolor='white',
                                                    alpha=0.8))
                #tmp_text.remove()
                self.draws.append(tmp_text)
            self.canvas.draw()
            for j in self.draws:
                j.remove()
            self.draws = []
        else:
            wx.MessageBox("模拟器中没有包含所选图像")
Ejemplo n.º 5
0
def pic_load_ram():
    #must run get handle first
    window_resolution = globalvar.get_window_resolution()
    max_resolution = globalvar.get_max_resolution()
    if window_resolution[0] == 0:
        raise Exception("未检测到模拟器")
    for keys, pic_path in config_ark.pic_confirm.items():
        temp_im = Image.open(pic_path)
        temp_im = temp_im.convert("RGB")
        width = int(window_resolution[0] / max_resolution[0] * temp_im.size[0])
        height = int(window_resolution[1] / max_resolution[1] *
                     temp_im.size[1])
        temp_im = temp_im.resize((width, height), Image.ANTIALIAS)
        config_ark.pic_confirm[keys] = np.array(temp_im)

    for keys, pic_path in config_ark.pic_huodong.items():
        temp_im = Image.open(pic_path)
        temp_im = temp_im.convert("RGB")
        width = int(window_resolution[0] / max_resolution[0] * temp_im.size[0])
        height = int(window_resolution[1] / max_resolution[1] *
                     temp_im.size[1])
        temp_im = temp_im.resize((width, height), Image.ANTIALIAS)
        config_ark.pic_huodong[keys] = np.array(temp_im)

    for keys, pic_path in config_ark.pic_where.items():
        temp_im = Image.open(pic_path)
        temp_im = temp_im.convert("RGB")
        width = int(window_resolution[0] / max_resolution[0] * temp_im.size[0])
        height = int(window_resolution[1] / max_resolution[1] *
                     temp_im.size[1])
        temp_im = temp_im.resize((width, height), Image.ANTIALIAS)
        config_ark.pic_where[keys] = np.array(temp_im)

    for keys, pic_path in config_ark.staff_pic.items():
        temp_im = Image.open(pic_path)
        temp_im = temp_im.convert("RGB")
        width = int(window_resolution[0] / max_resolution[0] * temp_im.size[0])
        height = int(window_resolution[1] / max_resolution[1] *
                     temp_im.size[1])
        temp_im = temp_im.resize((width, height), Image.ANTIALIAS)
        config_ark.staff_pic[keys] = np.array(temp_im)

    for keys, pic_path in config_ark.guanqia_pic.items():
        temp_im = Image.open(pic_path)
        temp_im = temp_im.convert("RGB")
        width = int(window_resolution[0] / max_resolution[0] * temp_im.size[0])
        height = int(window_resolution[1] / max_resolution[1] *
                     temp_im.size[1])
        temp_im = temp_im.resize((width, height), Image.ANTIALIAS)
        config_ark.guanqia_pic[keys] = np.array(temp_im)
    #常量点 自定义分辨率适应
    for keys, values in config_ark.points.items():
        if isinstance(values[0], int):
            width = int(window_resolution[0] / max_resolution[0] * values[0])
            height = int(window_resolution[1] / max_resolution[1] * values[1])
            config_ark.points[keys] = [width, height]
        else:
            for index, value_temp in enumerate(values):
                width = int(window_resolution[0] / max_resolution[0] *
                            value_temp[0])
                height = int(window_resolution[1] / max_resolution[1] *
                             value_temp[1])
                values[index] = [width, height]
            config_ark.points[keys] = values