示例#1
0
def run(imagepath):
    #
    enhancer = Enhancer()
    enhancer.enhance(imagepath)

    ##image = Image.open('pill3.jpg')
    ##image = ImageEnhance.Contrast(image).enhance(5)
    ##image.PIL.save("pic.jpg")

    ##quit()
    # get the labels from the Google Vision OCR
    vision = Vision()
    labels = vision.detect_document(imagepath)
    print(labels)
    # use Google Vision to detect dominant colors, then extract common color name using a custom library
    colors = vision.detect_properties(imagepath)
    print(colors)
    if len(labels) == 0 or len(colors) == 0:
        print('Could not classify')
        return 'Unidentified'

    api_call = "https://datadiscovery.nlm.nih.gov/resource/crzr-uvwg.json?" + "&splimprint=" + labels[
        0]
    # concatinate more if there are more than 1 imprints
    for i in range(1, len(labels)):
        api_call = api_call + ";" + labels[i]
    print(api_call)
    # make the API GET request
    contents = urllib.request.urlopen(api_call).read()
    # converts the bytes object to string, then to json
    json_response = json.loads(contents.decode())
    # get the number of matches
    if len(json_response) >= 1:
        return json_response[0]['medicine_name']
    elif len(json_response) < -1:
        json_res = try_with_colors(api_call, colors)
        if json_res == None:
            return 'Unidentified'
        else:
            return json_res[0]['medicine_name']
    else:
        return 'Unidentified'