使用Python簡單地去輔助百萬答題

janzyxcode發表於2018-01-24

前段時間地跳一跳助手很有意思,使用facebook-wda去做一下自動化地小功能還是很有意思地。

思路

facebook-wda能截圖,那把答題區域裁剪,想辦法識別其中地文字,提取出來,搜尋一下。試了一下Tesseract,效果不是很好,就用次數免費地api了,直接上程式碼。


client = AipOcr(APP_ID, API_KEY, SECRET_KEY)


c = wda.Client('http://192.168.1.103:8100')
s = c.session()
print(s.window_size())


def printNowDatetime():
    print(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))

def getImage(url):
    with open(url,'rb') as fp:
        return fp.read()

def ocrImage(image):
    # image = getImage('/Users/user/Desktop/testP.png')

    """ 如果有可選引數 """
    options = {}
    options["language_type"] = "CHN_ENG"
    options["detect_direction"] = "true"
    options["detect_language"] = "true"
    options["probability"] = "true"

    """ 帶引數呼叫通用文字識別, 圖片引數為本地圖片 """
    response = client.basicGeneral(image, options)
    print(response)
    print(type(response))
    words = response['words_result']
    appendWord = ''
    for item in words:
        appendWord += item['words'] + ''
    return appendWord


def cvCutImg(x,y,width,height,img):
    return img[y:y+height, x:x+width]

def cvBytes_to_numpyNdarray(imgBytes):
    img = np.asarray(bytearray(imgBytes), np.uint8)
    img = cv2.imdecode(img, cv2.IMREAD_COLOR)
    # cv2.imshow('mm', img)
    # cv2.waitKey(0)

    # img type is numpy.ndarray
    # img = cv2.imread('/Users/user/Desktop/testP.png')
    return img

def cvNumpyNdarray_to_bytes(img):
    return np.ndarray.tobytes(img)


def chongdingdahui():
    img = c.screenshot('screen01.png')
    # img = getImage('chongdingdahui.png')
    image = cvBytes_to_numpyNdarray(img)
    cutImg = cvCutImg(25, 320, 700, 175, image)
    cv2.imwrite('cut.png', cutImg)
    image = getImage('cut.png')
    ocrwd = ocrImage(image)
    image = getImage('cut.png')
    ocrwd = ocrImage(image)
    wd = parse.quote(ocrwd)
    url = 'https://www.baidu.com/s?wd=' + wd
    webbrowser.open(url)


def xiguashiping():
    img = c.screenshot('screen01.png')
    # img = getImage('xiguaishipin.png')
    image = cvBytes_to_numpyNdarray(img)
    cutImg = cvCutImg(40, 220, 670, 175, image)
    cv2.imwrite('cut.png', cutImg)
    image = getImage('cut.png')
    ocrwd = ocrImage(image)
    image = getImage('cut.png')
    ocrwd = ocrImage(image)
    wd = parse.quote(ocrwd)
    url = 'https://www.baidu.com/s?wd=' + wd
    webbrowser.open(url)


def zhishichaoren():
    img = c.screenshot('screen01.png')
    # img = getImage('zhishichaoren.png')
    image = cvBytes_to_numpyNdarray(img)
    cutImg = cvCutImg(40, 200, 670, 200, image)
    cv2.imwrite('cut.png', cutImg)
    image = getImage('cut.png')
    ocrwd = ocrImage(image)
    image = getImage('cut.png')
    ocrwd = ocrImage(image)
    wd = parse.quote(ocrwd)
    url = 'https://www.baidu.com/s?wd=' + wd
    webbrowser.open(url)


if __name__ == "__main__":
    while True:
        #time.sleep(0.5)
        printNowDatetime()
        # chongdingdahui()
        xiguashiping()
        # zhishichaoren()


複製程式碼

相關文章