Exemple #1
0
    def image_to_text(self, imagestream):

        result = []
        computervision_client = ComputerVisionClient(
            self.endpoint, CognitiveServicesCredentials(self.subscription_key))
        # Get an image with text
        recognize_handw_results = computervision_client.read_in_stream(
            imagestream, raw=True)
        # Get the operation location (URL with an ID at the end) from the response
        operation_location_remote = recognize_handw_results.headers[
            "Operation-Location"]
        # Grab the ID from the URL
        operation_id = operation_location_remote.split("/")[-1]
        while True:
            get_handw_text_results = computervision_client.get_read_result(
                operation_id)
            if get_handw_text_results.status not in ['notStarted', 'running']:
                break
            time.sleep(1)
        if get_handw_text_results.status == OperationStatusCodes.succeeded:
            for text_result in get_handw_text_results.analyze_result.read_results:
                for line in text_result.lines:
                    result.append(line.text)

        return result
def My_OCR(url):

    SUBSCRIPTION_KEY = os.getenv('OCR_SUBSCRIPTION_KEY')
    ENDPOINT = os.getenv('OCR_ENDPOINT')
    CV_CLIENT = ComputerVisionClient(
        ENDPOINT, CognitiveServicesCredentials(SUBSCRIPTION_KEY))

    ocr_results = CV_CLIENT.read(url, raw=True)
    operation_location_remote = ocr_results.headers["Operation-Location"]
    operation_id = operation_location_remote.split("/")[-1]

    status = ["notStarted", "running"]
    while True:
        get_handw_text_results = CV_CLIENT.get_read_result(operation_id)
        if get_handw_text_results.status not in status:
            break
        time.sleep(1)

    succeeded = OperationStatusCodes.succeeded

    text = []
    if get_handw_text_results.status == succeeded:
        res = get_handw_text_results.analyze_result.read_results
        for text_result in res:
            for line in text_result.lines:
                if len(line.text) <= 8:
                    text.append(line.text)

    r = re.compile("[0-9A-Z]{2,4}[.-]{1}[0-9A-Z]{2,4}")
    text = list(filter(r.match, text))

    return text[0].replace('.', '-') if len(text) > 0 else ""
def azure_get_data():
    subscription_key = "ACCESS KEY"
    endpoint = "ENDPOINT URL"
    computervision_client = ComputerVisionClient(
        endpoint, CognitiveServicesCredentials(subscription_key))
    #remote_image_url = "https://raw.githubusercontent.com/MicrosoftDocs/azure-docs/master/articles/cognitive-services/Computer-vision/Images/readsample.jpg"

    # Provide the image path
    local_image_handwritten_path = "sample4.jpg"
    local_image_handwritten = open(local_image_handwritten_path, "rb")
    recognize_handwriting_results = computervision_client.read_in_stream(
        local_image_handwritten, raw=True)
    operation_location_remote = recognize_handwriting_results.headers[
        "Operation-Location"]
    # Get Operation ID
    operation_id = operation_location_remote.split("/")[-1]

    # Repeat if Operation ID result status is 'notStarted' or 'running'
    while True:
        get_handw_text_results = computervision_client.get_read_result(
            operation_id)
        if get_handw_text_results.status not in ['notStarted', 'running']:
            break
        time.sleep(1)

    # Print the detected text, line by line
    if get_handw_text_results.status == OperationStatusCodes.succeeded:
        print(type(get_handw_text_results.analyze_result.read_results))
        for text_result in get_handw_text_results.analyze_result.read_results:
            for line in text_result.lines:
                print(line.text)
Exemple #4
0
def extractFromHandwritten(file_name):
    # Read the image file
    image_path = os.path.join('static', 'uploads', file_name)
    image_stream = open(image_path, "rb")
    resultString = ""

    # Get a client for the computer vision service
    computervision_client = ComputerVisionClient(
        cog_endpoint, CognitiveServicesCredentials(cog_key))

    # Submit a request to read printed text in the image and get the operation ID
    read_operation = computervision_client.read_in_stream(image_stream,
                                                          raw=True)
    operation_location = read_operation.headers["Operation-Location"]
    operation_id = operation_location.split("/")[-1]

    # Wait for the asynchronous operation to complete
    while True:
        read_results = computervision_client.get_read_result(operation_id)
        if read_results.status not in [OperationStatusCodes.running]:
            break
        time.sleep(1)

    # If the operation was successfuly, process the text line by line
    if read_results.status == OperationStatusCodes.succeeded:
        for result in read_results.analyze_result.read_results:
            for line in result.lines:
                resultString += line.text + "\n"
                print(line.text)

    return resultString
Exemple #5
0
    def read_image_text(self, client: ComputerVisionClient, remote_url: str):
        """
        read text from an image using the Azure OCR Read API
        """
        print(f"Calling read API on {remote_url}")

        read_result = client.read(remote_url, raw=True)

        operation_location_remote = read_result.headers['Operation-Location']
        operation_id = operation_location_remote.split('/')[-1]

        # GET method for read results
        while True:
            read_operation_result = client.get_read_result(operation_id)
            if read_operation_result.status not in [
                OperationStatusCodes.not_started,
                    OperationStatusCodes.running]:
                break
            time.sleep(1)  # Re-check every second

        return read_operation_result if read_operation_result.status ==\
            OperationStatusCodes.succeeded else None
Exemple #6
0
def ocr1(img):

    #Cognitive Services endpoint and key
    cog_key = '<Your Primary Key here>'  #Paste your primary key here
    cog_endpoint = '<Endpoint url here>'  #Paste your endpoint here

    # Get a client for the computer vision service
    computervision_client = ComputerVisionClient(
        cog_endpoint, CognitiveServicesCredentials(cog_key))

    # Submit a request to read printed text in the image and get the operation ID
    try:
        recognize_handw_results = computervision_client.read(img, raw=True)
    except Exception as e:
        return str(e)

    operation_location_remote = recognize_handw_results.headers[
        "Operation-Location"]
    operation_id = operation_location_remote.split("/")[-1]

    while True:
        get_handw_text_results = computervision_client.get_read_result(
            operation_id)
        if get_handw_text_results.status not in ['notStarted', 'running']:
            break
        time.sleep(1)

    if get_handw_text_results.status == OperationStatusCodes.succeeded:
        res = []
        for text_result in get_handw_text_results.analyze_result.read_results:
            for line in text_result.lines:
                res.append(str(line.text))

    if res != []:
        res = str(" ".join(res))  #Result of OCR
    else:
        res = None

    return res
subscription_key = "7a2607ca622b4e449763a5417111f329"
endpoint = "https://bayern.cognitiveservices.azure.com/"
img_path = 'C:\\Users\\cjjun\\Desktop\\img2.jpg'

client = ComputerVisionClient(endpoint,
                              CognitiveServicesCredentials(subscription_key))

img = open(img_path, 'rb')
recognize_results = client.read_in_stream(img, raw=True)
img.close()

location_remote = recognize_results.headers["Operation-Location"]
operation_id = location_remote.split("/")[-1]

while True:
    result = client.get_read_result(operation_id)
    if result.status not in ['notStarted', 'running']:
        break
    sleep(0.5)

img_text = cv2.imread(img_path)
if result.status == OperationStatusCodes.succeeded:
    for text_result in result.analyze_result.read_results:
        for line in text_result.lines:
            box = [int(i) for i in line.bounding_box]
            print(line.text)

            for i in range(0, 8, 2):
                pt1 = (*box[i:i + 2], )
                pt2 = (*box[(i + 2) % 8:(i + 3) % 8 + 1], )
                cv2.line(img_text, pt1, pt2, (255, 0, 0), 3)
Exemple #8
0
def InvAns(strUrl):
    
    if 'COMPUTER_VISION_SUBSCRIPTION_KEY' in os.environ:
        subscription_key = os.environ['COMPUTER_VISION_SUBSCRIPTION_KEY']
    else:
        print("\nSet the COMPUTER_VISION_SUBSCRIPTION_KEY environment variable.\n**Restart your shell or IDE for changes to take effect.**")
        sys.exit()

    if 'COMPUTER_VISION_ENDPOINT' in os.environ:
        endpoint = os.environ['COMPUTER_VISION_ENDPOINT']
    else:
        print("\nSet the COMPUTER_VISION_ENDPOINT environment variable.\n**Restart your shell or IDE for changes to take effect.**")
        sys.exit()

    computervision_client = ComputerVisionClient(endpoint, CognitiveServicesCredentials(subscription_key))


    # print("===== Batch Read File - remote =====")

    remote_image_handw_text_url = strUrl

    recognize_handw_results = computervision_client.read(remote_image_handw_text_url,  raw=True)

    operation_location_remote = recognize_handw_results.headers["Operation-Location"]

    operation_id = operation_location_remote.split("/")[-1]

    while True:
        get_handw_text_results = computervision_client.get_read_result(operation_id)
        if get_handw_text_results.status not in ['notStarted', 'running']:
            break
        time.sleep(1)
    d = {}
    abcA = []
    defgA = []
    hijA = []
    klmA = []
    nopA= []
    qrsA = []
    uvwA = []
    flat_list = []
    if get_handw_text_results.status == OperationStatusCodes.succeeded:
        for text_result in get_handw_text_results.analyze_result.read_results:
            
            for sourceTxt in text_result.lines:
                new_d = {}
                new_d[sourceTxt.text] = sourceTxt.bounding_box
                flat_list.append(sourceTxt.text)


                def Get_shipping_in_pdf(sourceTxt):
                    # print(cf)
                    shp = r"Shipping.*\d.*$"
                    shp1 = r"Shipping"
                    if (re.findall(shp, sourceTxt)):
                        ans = re.findall(shp, sourceTxt)[0]
                        f_ans_split = ans.split()
                        for i in f_ans_split:
                            if i.isnumeric():
                                print(i)
                                f_ans = i
                            else:
                                f_ans = ''
                    elif (re.findall(shp1, sourceTxt)):
                        f_ans = re.findall(shp1, sourceTxt)[0]   
                    else:
                        f_ans = "0"
                    return f_ans
                
                def Get_company_in_pdf(sourceTxt):
                    frm = r"From:"
                    frm1 = r"From"
                    frm2= r"Invoice"
                    if(re.findall(frm, sourceTxt)):
                        f_ans = re.findall(frm, sourceTxt)[0]
                    elif(re.findall(frm1, sourceTxt)):
                        f_ans = re.findall(frm1, sourceTxt)[0]
                    elif(re.findall(frm2, sourceTxt)):
                        f_ans = re.findall(frm2, sourceTxt)[0]
                    else:
                        f_ans = 0
                    # print(f_ans)    
                    return f_ans
                
                def Get_order_in_pdf(sourceTxt):
                    prcnt = r"Purchase.*\d.*$"
                    prcnt1 = r"Ord.*\d.*$"
                    prcnt2 = r"Ord.*\D"
                    if (re.findall(prcnt, sourceTxt)):
                        ans = re.findall(prcnt, sourceTxt)[0]
                        f_ans_split = ans.split()
                        for i in f_ans_split:
                            if i.isnumeric():
                                f_ans = i
                            else:
                                f_ans = ''
                    elif(re.findall(prcnt1, sourceTxt)):
                        ans = re.findall(prcnt1, sourceTxt)[0]
                        f_ans_split = ans.split()
                        for i in f_ans_split:
                            if i.isnumeric():
                                f_ans = i
                            else:
                                f_ans = ''
                    elif (re.findall(prcnt2, sourceTxt)):
                        f_ans = re.findall(prcnt2, sourceTxt)[0]   
                    else:
                        f_ans = ''
                    return f_ans

                def Get_invoice_in_pdf(sourceTxt):
                    
                    prcnt = r"Invoice.*\d.*$"
                    prcnt1 =  r"INV.*\-\d.*$"
                    f_ans = ''
                    if (re.findall(prcnt1, sourceTxt)):
                        f_ans = re.findall(prcnt1, sourceTxt)[0]
                    elif(re.findall(prcnt, sourceTxt)):
                        ans = re.findall(prcnt, sourceTxt)[0]
                        f_ans_split = ans.split()
                        for i in f_ans_split:
                            if i.isnumeric():
                                f_ans = i 
                            else:
                                f_ans = '' 
                    else:
                        f_ans = '' 
                    return f_ans

                def Get_vat_in_document(sourceTxt):
                    
                    prcnt = r"([0-9][0-9]%$|[0-9]%$)"

                    if(re.findall(prcnt, sourceTxt)):
                        f_ans = re.findall(prcnt, sourceTxt)[0]
                    else:
                        f_ans = 0
        
                    return f_ans
                
                def Get_total_amount_in_pdf(sourceTxt):
                    lst = r"\d.*,\d{3}\.?\d*$"
                    dcm = r"\d.*\..?\d*$"
                    spc = r"\d.*\s\d{3}\.\d*$"
                    ans=[]
                    ttl = []
                    f_ans = []
                    if(re.findall(lst, sourceTxt)):
                        ans.extend(re.findall(lst, sourceTxt))
                    elif re.findall(dcm, sourceTxt):
                        ans.extend(re.findall(dcm, sourceTxt))
                    elif re.findall(spc, sourceTxt):
                        ans.extend(re.findall(spc, sourceTxt))    
                    if ans:
                        for i in ans:
                            rem_c = i.split(',')
                            try:
                                ttl.append(float("".join(rem_c)))
                            except ValueError:
                                pass    
                
                    if ttl == []:
                        pass
                    else:
                        f_ans.extend(ttl)   
                        
                    return f_ans
                    
                def Get_date_in_pdf(sourceTxt):
                    date_format = [ r"\d{2} (?:%s) \d{4}" % '|'.join(calendar.month_abbr[1:]),  r"\d{1} (?:%s) \d{4}" % '|'.join(calendar.month_abbr[1:]), r"\d{2} (?:%s) \d{4}" % '|'.join(calendar.month_name),  r"\d{1} (?:%s) \d{4}" % '|'.join(calendar.month_name[1:]), r"(?:%s) \d{2} \d{4}" % '|'.join(calendar.month_abbr[1:]),  r"(?:%s) \d{1} \d{4}" % '|'.join(calendar.month_abbr[1:]), r"(?:%s) \d{2} \d{4}" % '|'.join(calendar.month_name),  r"(?:%s) \d{1} \d{4}" % '|'.join(calendar.month_name[1:]),r"\d{2} (?:%s), \d{4}" % '|'.join(calendar.month_abbr[1:]),  r"\d{1} (?:%s), \d{4}" % '|'.join(calendar.month_abbr[1:]), r"\d{2} (?:%s), \d{4}" % '|'.join(calendar.month_name),  r"\d{1} (?:%s), \d{4}" % '|'.join(calendar.month_name[1:]), r"(?:%s) \d{2}, \d{4}" % '|'.join(calendar.month_abbr[1:]),  r"(?:%s) \d{1}, \d{4}" % '|'.join(calendar.month_abbr[1:]), r"(?:%s) \d{2}, \d{4}" % '|'.join(calendar.month_name),  r"(?:%s) \d{1}, \d{4}" % '|'.join(calendar.month_name[1:])]

                    dats = []
                    f_dats = ''
                    


                    for i in date_format:
                        if(re.findall(i, sourceTxt)):
                            dats.extend(re.findall(i, sourceTxt))
                        else:
                            ans = re.findall(r'([1-9]|1[0-9]|2[0-9]|3[0-1]|0[0-9])(.|-|\/)([1-9]|1[0-2]|2[0-9]|3[0-9])(.|-|\/)(20[0-9][0-9])',sourceTxt)
                            ans_f = [''.join(ans[i]) for i in range(len(ans))]
                            dats.extend(ans_f)
                    for sublist in dats:

                        if any(sublist): 
                            f_dats = sublist
                            break   
                    return f_dats

                
                abc = Get_date_in_pdf(sourceTxt.text)
                defg = Get_total_amount_in_pdf(sourceTxt.text)
                hij = Get_vat_in_document(sourceTxt.text)
                klm = Get_invoice_in_pdf(sourceTxt.text)
                nop = Get_order_in_pdf(sourceTxt.text)
                qrs = Get_company_in_pdf(sourceTxt.text)
                uvw = Get_shipping_in_pdf(sourceTxt.text)
                
                
                if abc=="" and defg=="" and hij:
                    continue
                elif abc:
                    abcA.append(abc)
                    continue
                elif  defg:
                    defgA.append(defg)
                    continue
                elif hij:
                    hijA.append(hij)
                elif klm:
                    klmA.append(klm)
                elif nop:
                    nopA.append(nop) 
                elif qrs:
                    print(qrs)
                    qrsA.append(qrs)
                elif uvw:
                    uvwA.append(uvw) 


            def get_index_ord(inp, lst): 
            
                indx = lst.index(inp)
                f_ans = lst[indx + 1]
                return f_ans
            def get_indx_purchase(inp, lst):
                indx = int(lst.index(inp))
                f_ans = ''
                for i in lst[indx:indx+4]:
                    if i.isnumeric():
                        f_ans = i
                        break
                    else:
                        f_ans =''

                return f_ans
            def get_company_name(inp,lst):

                idx = int(lst.index(inp))
                f_ans = ''
                f_indx = int(idx + 1)
                for i in lst[f_indx]:
                    if i.isnumeric():
                        f_ans = lst[f_indx + 1]
                    else:
                        f_ans = lst[f_indx]
                return f_ans
            def get_company_name_2(sublist, lst):
                f_ans = ''
                print(sublist)
                if "From:" in sublist:
                    f_ans = get_company_name("From:", lst)
                    print(f_ans)
                elif "From" in sublist:
                    f_ans = get_company_name("From", lst)
                elif "Invoice" in sublist:
                    f_ans = get_company_name("Invoice", lst)
                else:
                    f_ans =  lst[0]
                return f_ans  
            def Get_shipping_cost(inp, lst):
                f_ans = ''
                inpt = ''
                if inp:
                    inpt = inp[0]
                else:
                    inpt = "0"
            
                if inpt.isnumeric():
                    f_ans = inpt
                else:
                    indx = lst.index(inpt)
                    f_indx = int(indx + 1)
                    f_ans = lst[f_indx]
                    if f_ans.isnumeric():
                        f_ans = lst[f_indx]
                    else:
                        f_ans = 0
                return f_ans
            
            order_2 = get_index_ord(nopA[0], flat_list) 
            company_name = get_company_name_2(qrsA, flat_list)
            purchase_2 = get_indx_purchase("Invoice", flat_list)
                
        def vat_2(inp):
            ans_vat = ''
            new_set = list(set([i[0] for i in inp]))
            new_set.sort()
            if  new_set[-1] - new_set[-2] in new_set:
                ans_vat = new_set[-1] - new_set[-2] 
            else:
                ans_vat = 0
            return ans_vat
        vat_2ans = vat_2(defgA)
        shipping = int(Get_shipping_cost(uvwA, flat_list))
                
        
        # print(shipping)
        
        d["date"] = min(abcA)
        d["total_amount"] = max(defgA)[0]
        d["vat"] = vat_2ans - shipping if int(hijA[0][0:-1]) /100 * d["total_amount"] == 0 else int(hijA[0][0:-1]) /100 * d["total_amount"] - shipping
        d["invoice_number"] = klmA[0] if klmA[0] else purchase_2
        d["purchase_order_number"] = nopA[0] if nopA[0].isnumeric() else order_2
        d["supplier_name"] = company_name
    # print("Apiyo", d)
    return d
computervision_client = ComputerVisionClient(
    endpoint, CognitiveServicesCredentials(subscription_key))
remote_image_handw_text_url = "0.jpg"

# Call API with URL and raw response (allows you to get the operation location)
recognize_handw_results = computervision_client.read(
    remote_image_handw_text_url, raw=True)

# Get the operation location (URL with an ID at the end) from the response
operation_location_remote = recognize_handw_results.headers[
    "Operation-Location"]
# Grab the ID from the URL
operation_id = operation_location_remote.split("/")[-1]

# Call the "GET" API and wait for it to retrieve the results
while True:
    get_handw_text_results = computervision_client.get_read_result(
        operation_id)
    if get_handw_text_results.status not in ['notStarted', 'running']:
        break
    time.sleep(1)

results = []
# Print the detected text, line by line
if get_handw_text_results.status == OperationStatusCodes.succeeded:
    for text_result in get_handw_text_results.analyze_result.read_results:
        for line in text_result.lines:
            results.append(line.text)

print("re", results)
Exemple #10
0
# Get an image with text
read_image_url = "https://raw.githubusercontent.com/MicrosoftDocs/azure-docs/master/articles/cognitive-services/Computer-vision/Images/readsample.jpg"

# Call API with URL and raw response (allows you to get the operation location)
read_response = computervision_client.read(read_image_url,  raw=True)
# </snippet_read_call>

# <snippet_read_response>
# Get the operation location (URL with an ID at the end) from the response
read_operation_location = read_response.headers["Operation-Location"]
# Grab the ID from the URL
operation_id = read_operation_location.split("/")[-1]

# Call the "GET" API and wait for it to retrieve the results 
while True:
    read_result = computervision_client.get_read_result(operation_id)
    if read_result.status not in ['notStarted', 'running']:
        break
    time.sleep(1)

# Print the detected text, line by line
if read_result.status == OperationStatusCodes.succeeded:
    for text_result in read_result.analyze_result.read_results:
        for line in text_result.lines:
            print(line.text)
            print(line.bounding_box)
print()
# </snippet_read_response>
'''
END - Read File - remote
'''
Exemple #11
0
def main():
    tello = Tello()
    tello.connect()
    tello.streamon()

    frame_read = tello.get_frame_read()

    tello.takeoff()
    time.sleep(4)
    tello.send_rc_control(0, 0, 70, 0)
    time.sleep(1.5)
    tello.send_rc_control(0, 0, 0, 0)

    try:
        # Configure Azure Computer vision
        computervision_client = ComputerVisionClient(
            COGNITIVESVC_ENDPOINT,
            CognitiveServicesCredentials(SUBSCRIPTION_KEY))

        # Configure MediaPipe hands recognizer
        mp_drawing = mp.solutions.drawing_utils
        mp_hands = mp.solutions.hands
        hands = mp_hands.Hands(max_num_hands=2,
                               min_detection_confidence=0.8,
                               min_tracking_confidence=0.5)

        while True:
            # Get frame
            original_frame = frame_read.frame

            frame = cv2.flip(original_frame, 1)
            frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
            frame.flags.writeable = False  # Enabled pass by reference and improves performance

            num_of_fingers = -1
            results = hands.process(frame)
            if results.multi_hand_landmarks:
                for hand_landmarks in results.multi_hand_landmarks:
                    mp_drawing.draw_landmarks(frame, hand_landmarks,
                                              mp_hands.HAND_CONNECTIONS)

                    num_of_fingers = num_of_fingers + count_finger(
                        hand_landmarks, mp_hands.HandLandmark.PINKY_MCP,
                        mp_hands.HandLandmark.THUMB_TIP,
                        mp_hands.HandLandmark.THUMB_IP)

                    num_of_fingers = num_of_fingers + count_finger(
                        hand_landmarks, mp_hands.HandLandmark.WRIST,
                        mp_hands.HandLandmark.INDEX_FINGER_TIP,
                        mp_hands.HandLandmark.INDEX_FINGER_PIP)

                    num_of_fingers = num_of_fingers + count_finger(
                        hand_landmarks, mp_hands.HandLandmark.WRIST,
                        mp_hands.HandLandmark.MIDDLE_FINGER_TIP,
                        mp_hands.HandLandmark.MIDDLE_FINGER_PIP)

                    num_of_fingers = num_of_fingers + count_finger(
                        hand_landmarks, mp_hands.HandLandmark.WRIST,
                        mp_hands.HandLandmark.RING_FINGER_TIP,
                        mp_hands.HandLandmark.RING_FINGER_PIP)

                    num_of_fingers = num_of_fingers + count_finger(
                        hand_landmarks, mp_hands.HandLandmark.WRIST,
                        mp_hands.HandLandmark.PINKY_TIP,
                        mp_hands.HandLandmark.PINKY_PIP)

            print("Number of fingers: " + str(num_of_fingers))

            # Show image
            cv2.imshow('Webcam', frame)

            # Exit when user press ESC key
            k = cv2.waitKey(3) & 0xFF
            if k == 27:  # ESC Key
                break

            tello.send_rc_control(0, 0, 0, 0)

            time.sleep(1)

            if num_of_fingers > 0 and num_of_fingers < 10:
                while num_of_fingers > 0:
                    # Get frame
                    ocr_frame = frame_read.frame
                    ocr_frame.flags.writeable = False

                    # Send frame to Microsoft Azure Cognitive Services to detect text in the image
                    _, buf = cv2.imencode(".jpg", ocr_frame)
                    stream = io.BytesIO(buf)
                    recognize_handw_results = computervision_client.read_in_stream(
                        stream, raw=True)

                    # OCR is async. Wait until is completed.
                    operation_location_remote = recognize_handw_results.headers[
                        "Operation-Location"]
                    operation_id = operation_location_remote.split("/")[-1]
                    while True:
                        get_handw_text_results = computervision_client.get_read_result(
                            operation_id)
                        if get_handw_text_results.status not in [
                                'notStarted', 'running'
                        ]:
                            break
                        tello.send_rc_control(0, 0, 0, 0)
                        time.sleep(1)

                    # Mark the detected text, line by line
                    xg = yg = wg = hg = None
                    if get_handw_text_results.status == OperationStatusCodes.succeeded:
                        for text_result in get_handw_text_results.analyze_result.read_results:
                            for line in text_result.lines:
                                for word in line.words:
                                    boundingbox = word.bounding_box
                                    if str(num_of_fingers) in word.text:
                                        xg, yg, wg, hg = (int(boundingbox[0]),
                                                          int(boundingbox[1]),
                                                          int(boundingbox[2] -
                                                              boundingbox[0]),
                                                          int(boundingbox[7] -
                                                              boundingbox[1]))
                                        cv2.rectangle(ocr_frame, (xg, yg),
                                                      (xg + wg, yg + hg),
                                                      (0, 255, 0), 2)
                                    else:
                                        nxg, nyg, nwg, nhg = (
                                            int(boundingbox[0]),
                                            int(boundingbox[1]),
                                            int(boundingbox[2] -
                                                boundingbox[0]),
                                            int(boundingbox[7] -
                                                boundingbox[1]))
                                        cv2.rectangle(ocr_frame, (nxg, nyg),
                                                      (nxg + nwg, nyg + nhg),
                                                      (0, 0, 255), 2)

                    cv2.imshow('Webcam', ocr_frame)

                    # Exit when user press ESC key
                    k = cv2.waitKey(3) & 0xFF
                    if k == 27:  # ESC Key
                        break

                    velocity_fb = velocity_lr = velocity_ud = velocity_yaw = 0
                    if not xg is None:
                        # Move the drone
                        object_center_x = int(xg + (wg / 2))
                        object_center_y = int(yg + (hg / 2))
                        object_size = ((wg**2) + (hg**2))**0.5  # Fast sqrt

                        object_distance = DESIRED_OBJECT_SIZE - object_size
                        if not object_distance == 0:
                            velocity_fb = int(
                                MAX_SPEED_FORWARDBACK *
                                (object_distance / DESIRED_OBJECT_SIZE))

                        frame_shape = ocr_frame.shape
                        # I wrote 'object_center_y + 200' because the camera of Tello drone is slightly inclined to down and that causes the drone to go too high
                        velocity_ud = calculate_velocity(
                            frame_shape[1], object_center_y + 200,
                            MAX_SPEED_UPDOWN * -1)
                        velocity_lr = calculate_velocity(
                            frame_shape[0], object_center_x, MAX_SPEED_LR)

                        if abs(velocity_fb) < 5 and abs(
                                velocity_ud) < 5 and abs(velocity_yaw) < 5:
                            time.sleep(5)
                            break

                        if not velocity_lr == velocity_fb == velocity_ud == velocity_yaw == 0:
                            tello.send_rc_control(velocity_lr, velocity_fb,
                                                  velocity_ud, velocity_yaw)

                time.sleep(MOV_TIME)
                tello.send_rc_control(0, 0, 0, 0)
    finally:
        tello.land()
        tello.streamoff()
        tello.end()

        # When everything done, release the capture
        cv2.destroyAllWindows()
print("===== Read File - local =====")
# Get image of handwriting
local_image_handwritten_path = os.path.join (images_folder, "handwritten_text.jpg")
# Open the image
local_image_handwritten = open(local_image_handwritten_path, "rb")

# Call API with image and raw response (allows you to get the operation location)
recognize_handwriting_results = computervision_client.read_in_stream(local_image_handwritten, raw=True)
# Get the operation location (URL with ID as last appendage)
operation_location_local = recognize_handwriting_results.headers["Operation-Location"]
# Take the ID off and use to get results
operation_id_local = operation_location_local.split("/")[-1]

# Call the "GET" API and wait for the retrieval of the results
while True:
    recognize_handwriting_result = computervision_client.get_read_result(operation_id_local)
    if recognize_handwriting_result.status.lower () not in ['notstarted', 'running']:
        break
    print ('Waiting for result...')
    time.sleep(10)

# Print results, line by line
if recognize_handwriting_result.status == OperationStatusCodes.succeeded:
    for text_result in recognize_handwriting_result.analyze_result.read_results:
        for line in text_result.lines:
            print(line.text)
            print(line.bounding_box)
print()
'''
END - Read File - local
'''
Exemple #13
0
# Call API with URL and raw response (allows you to get the operation location)
recognize_printed_results = computervision_client.read(
    remote_image_printed_text_url, language='en', raw=True)
# </snippet_read_call>

# <snippet_read_response>
# Get the operation location (URL with an ID at the end) from the response
operation_location_remote = recognize_printed_results.headers[
    "Operation-Location"]
# Grab the ID from the URL
operation_id = operation_location_remote.split("/")[-1]

# Call the "GET" API and wait for it to retrieve the results
while True:
    get_printed_text_results = computervision_client.get_read_result(
        operation_id)
    if get_printed_text_results.status not in ['NotStarted', 'Running']:
        break
    time.sleep(1)

# Print the detected text, line by line
if get_printed_text_results.status == TextOperationStatusCodes.succeeded:
    for text_result in get_printed_text_results.recognition_results:
        for line in text_result.lines:
            print(line.text)
            print(line.bounding_box)
print()
# </snippet_read_response>
'''
END - Batch Read File - remote
'''
def azure_batch_read_in_stream(filename=None, callOCR=True, verbose=False):
    """RecognizeTextUsingBatchReadAPI.
    This will recognize text of the given image using the Batch Read API.
    """
    import time
    #
    # Azure Specific
    #
    SUBSCRIPTION_KEY_ENV_NAME = os.environ.get(
        "COMPUTERVISION_SUBSCRIPTION_KEY", None)
    COMPUTERVISION_LOCATION = os.environ.get("COMPUTERVISION_LOCATION",
                                             "westeurope")

    azure_client = ComputerVisionClient(
        endpoint="https://" + COMPUTERVISION_LOCATION +
        ".api.cognitive.microsoft.com/",
        credentials=CognitiveServicesCredentials(SUBSCRIPTION_KEY_ENV_NAME))
    print("AZURE Image Name {}".format(filename))
    p = Path(filename)
    (imgname, imgext) = os.path.splitext(p.name)

    # Check if we have a cached ocr response already for this provider
    invokeOCR = callOCR
    if not callOCR:
        if not os.path.exists(
                os.path.join(RESULTS_FOLDER, imgname + ".azure.read.json")):
            invokeOCR = True

    if invokeOCR:
        # Azure Computer Vision Call
        # with open(os.path.join(IMAGES_FOLDER, filename), "rb") as image_stream:
        with open(filename, "rb") as image_stream:
            job = azure_client.read_in_stream(image=image_stream, raw=True)
        operation_id = job.headers['Operation-Location'].split('/')[-1]

        image_analysis = azure_client.get_read_result(operation_id, raw=True)
        while image_analysis.output.status in ['notstarted', 'running']:
            time.sleep(1)
            image_analysis = azure_client.get_read_result(
                operation_id=operation_id, raw=True)
        print("\tJob completion is: {}".format(image_analysis.output.status))
        print("\tRecognized {} page(s)".format(
            len(image_analysis.output.analyze_result.read_results)))

        with open(os.path.join(RESULTS_FOLDER, imgname + ".azure.read.json"),
                  'w') as outfile:
            outfile.write(image_analysis.response.content.decode("utf-8"))
        ocrresponse = image_analysis.response.content.decode("utf-8")
    else:
        # Use local OCR cached response when available
        with open(os.path.join(RESULTS_FOLDER, imgname + ".azure.read.json"),
                  'r') as cachefile:
            ocrresponse = cachefile.read().replace('\n', '')

    # Convert the original ocrresponse into proper object
    ocrresponse = BBOXOCRResponse.from_azure(json.loads(ocrresponse))

    # load the original response to get the text as-is
    original_text = ""
    for page in ocrresponse.pages:
        for line in page.lines:
            original_text += (line.text)
            original_text += ('\n')

    with open(os.path.join(RESULTS_FOLDER, imgname + ".before.azure.read.txt"),
              'w') as outfile:
        outfile.write(original_text)

    # Create BBOX OCR Response from Azure CV string response
    bboxresponse = BBoxHelper(
        verbose=verbose).processAzureOCRResponse(ocrresponse)
    with open(os.path.join(RESULTS_FOLDER, imgname + ".azure.bbox.json"),
              'w') as outfile:
        outfile.write(
            json.dumps(bboxresponse.__dict__,
                       default=lambda o: o.__dict__,
                       indent=4))

    with open(os.path.join(RESULTS_FOLDER, imgname + ".after.azure.read.txt"),
              'w') as outfile:
        outfile.write(bboxresponse.text)

    return (original_text, bboxresponse.text)