def run(file, lang): try: imgFile = np.array(PIL.Image.open(file).convert("RGB")) if lang == 'eng': reader = easyocr.Reader(['en']) else: reader = easyocr.Reader([lang, 'en']) text = reader.readtext(imgFile) res_list = list() for i in text: res_list.append(i[1]) res_str = '' for i in res_list: res_str = res_str + ' ' + i imgFile = PIL.Image.fromarray(imgFile) img_io = io.BytesIO() imgFile.save(img_io, 'jpeg', quality=100) img_io.seek(0) img = base64.b64encode(img_io.getvalue()) return [res_str, img] except Exception as e: print("error : ", e) return 500
class OCRReader: sim_reader = easyocr.Reader(["ch_sim", "en"]) tra_reader = easyocr.Reader(["ch_tra", "en"]) reader_modes = {"traditional": tra_reader, "simplified": sim_reader} def __init__(self): pass def read_image(self, file_path, reader="simplified"): return "".join(OCRReader.reader_modes[reader].readtext(file_path, detail=0))
def get_ocr_result(img, x1y1, x2y2): reader = easyocr.Reader(['en']) img_tmp = "image_%d.jpg" % random.randint(1, 10) cv2.imwrite(img_tmp, (img.numpy() * 255).astype(int)) result = reader.readtext(img_tmp, detail=0) return ' '.join(result)
def process_ocr(): docs = Document.objects.filter(ocr_processed=False) _logger.info('=======Process OCR Task========') buffer_image = '/code/rest/mediafiles/buffer.jpg' for doc in docs: path = doc.file.path text = '' reader = easyocr.Reader(lang_list=['pt']) if doc.file: if doc.mimetype == 'application/pdf': pages = convert_from_path(path, 500) for page in pages: page.save(buffer_image, 'JPEG') data = reader.readtext(buffer_image) for element in data: text += element[1] + ' ' elif doc.mimetype in ['image/jpeg', 'image/png']: data = reader.readtext(path) for element in data: text += element[1] + ' ' if text: doc.text = text doc.ocr_processed = True doc.save() _logger.info('=======End Process OCR Task========')
def temp_and_ocr(thermo_photo): """Validate content of thermo photo""" thermo = settings.MEDIA_ROOT + str(Path(thermo_photo.thermo_picture.url[6:])) temp = [] res = calculate_temp_bbox(thermo) for i in range(len(res)): probe_img = cv2.imread(settings.MEDIA_ROOT + str(Path(thermo_photo.thermo_picture.url[6:]))) probe_img = probe_img[res[i]['y1']:res[i]['y2'], res[i]['x1']:res[i]['x2']] reader = easyocr.Reader(['th','en']) bounds = reader.readtext(probe_img) try: temp.append(float(bounds[0][1])) thermo_photo.is_active = True thermo_photo.save() except: thermo_photo.is_active = False thermo_photo.save() send_fail_email( thermo_photo.report.user.pk, thermo_photo.report.pk, thermo_photo.pk ) return thermo_photo.RTMax = max(temp) thermo_photo.RTMin = min(temp) thermo_photo.RTMean = ((max(temp) + min(temp)) / 2) thermo_photo.is_valid = True thermo_photo.save()
def test(): leitor = easyocr.Reader(['pt']) resultados = leitor.readtext('images/dbg.png', paragraph=False) for palavra in resultados: print(f'Palavra: {palavra[0]}\n' f'Posição em relação a imagem: {palavra[1]}\n')
def test_easyocr_artifact_packs(): svc = EasyOCRService() lang_list = ['ch_sim', 'en'] recog_network = "zh_sim_g2" model = easyocr.Reader( lang_list=lang_list, gpu=False, download_enabled=True, recog_network=recog_network, ) svc.pack('chinese_small', model, lang_list=lang_list, recog_network=recog_network) assert [x[1] for x in model.readtext(IMAGE_PATH) ] == (TEST_RESULT), 'Run inference before saving the artifact' saved_path = svc.save() loaded_svc = bentoml.load(saved_path) assert loaded_svc.predict(imageio.imread(IMAGE_PATH))['text'] == ( TEST_RESULT), 'Run inference after saving the artifact' # clean up saved bundle yc = YataiClient() yc.repository.delete(f'{svc.name}:{svc.version}')
def detectImage(): try: time.sleep(0.5) ImageFile.LOAD_TRUNCATED_IMAGES = True PIC_PATH = 'image_for_detection/image.jpg' response = requests.get( "http://www.041er-blj.ch/2020/dakapka/notewriter/image.jpg", stream=True) file = open(PIC_PATH, "wb") response.raw.decode_content = True shutil.copyfileobj(response.raw, file) del response reader = easyocr.Reader(['en', 'de'], gpu=False) result = reader.readtext(PIC_PATH, detail=0) print(result) result_string = '' for word in result: result_string += word + ' ' print(result_string) os.remove('image_for_detection/image.jpg') return result_string except: return 'Oops! Seems like there was an Error.\nPlease Try again.'
def main(): vc = WebcamVideoStream(src=0).start() reader = easyocr.Reader(['en']) while True: # read the current camera frame frame = vc.read() # show the current frame (untouched) cv2.imshow("My webcam", frame) # if 'x' key is pressed, exit the loop if cv2.waitKey(1) & 0XFF == ord('x'): break # if 'c' key is pressed, process the frame for OCR if cv2.waitKey(1) & 0xFF == ord('c'): # copy the image so it will be a cleaned image for tesseract img = vc.mask_frame() # DEBUG img_box = draw_boxes(img, reader) # draw the chart containing the image with boxes cv2.imshow("Tesseract", img_box) print_img_str(img, reader) # close the window and de-allocate any associated memory usage cv2.destroyAllWindows() # close the already opened camera vc.stop()
def orc(request): import easyocr url = request.GET.get('url', '') if url: reader = easyocr.Reader(['ch_sim', 'en']) # need to run only once to load model into memory result = reader.readtext(url, detail=0) return HttpResponse(result)
def predict_meme_class(clf, meme_path): reader = easyocr.Reader( ['en']) # need to run only once to load model into memory sentence = reader.readtext(meme_path) result = [] for text in sentence: result.append(text[1]) sentence = ' '.join(result) x_text = extract_features(word_embedder, tokenizer, sentence, meme_path, vgg_model) x_text = np.reshape(x_text, (1, 4396)) predict_label = clf.predict(x_text) prediction = np.argmax(predict_label) if prediction == 0: print("Non-hateful") elif prediction == 1: print("Hateful") return
def __init__(self): self.device = True if torch.cuda.is_available() else False self.list_langs = ['en', 'id'] self.recog_network = 'english_g2' self.reader = easyocr.Reader(self.list_langs, gpu=self.device, recog_network=self.recog_network)
def server(port): class SimpleServerFactory(ServerFactory): reader = None def __init__(self, reader): self.reader = reader class SimpleReceiver(LineReceiver, ABC): def connectionMade(self): logger.info('Got connection from %s', self.transport.client) def connectionLost(self, reason): logger.info('%s disconnected', self.transport.client) def dataReceived(self, data): if type(data) == bytes: logger.info("Received data ...") else: logger.info("Received data:%s", data) result = reader.readtext(data, detail=0) str = '' for item in result: str = str + item self.sendLine(str.encode('utf-8')) logger.info("Send response: %s", str) # need to run only once to load model into memory reader = easyocr.Reader(['ch_sim', 'en']) factory = SimpleServerFactory(reader) factory.protocol = SimpleReceiver reactor.listenTCP(port, factory) logger.info("TCP server started on port(s): %s ..." % (port)) reactor.run()
def load(self, event): time_start = time.time() address = self.filename.GetValue() reader = easyocr.Reader( ['ch_sim', 'en']) # need to run only once to load model into memory result = reader.readtext(address) time_end = time.time() #print(result) res = "Time Cost: " + str(time_end - time_start) + ' s\n' List = [] for line in result: res = res + line[1] + '\n' List.append(line[1]) print(List) self.contents.SetValue(res) #填充文本框 rels = kg.extract(text=List) g = kg.rel2graph(rels) ## 依据文本解析结果, 生成networkx有向图 fig = kg.graph2fig(g, x=1500, y=1500) ## 生成 matplotlib.figure.Figure 图片 fig.savefig("KG_test_output.png") img = cv2.imread("KG_test_output.png") img = img[200:1500, :, :] cv2.imwrite("KG_test_output.png", img) img = wx.Image("KG_test_output.png", wx.BITMAP_TYPE_PNG).Rescale(750, 650) img = img.ConvertToBitmap() self.bitmap.SetBitmap(img)
def start_ocr(): print('Start OCR License Plate : ') reader = easyocr.Reader(['ko']) result = None temp = None for a in range(0,5) : str_a = str(a) os.remove('media/video/result/frame/detected'+str_a+'.jpg') for i in range(index2) : str_i = str(i+5) if result is not None: temp = result result = reader.readtext('media/video/result/frame/detected'+str_i+'.jpg', detail = 0) str_result = "\n".join(result) if temp != result : with open("media/video/result/번호판.txt", "a") as f: f.write(str_i) f.write(':') f.write(str_result) f.write('\n') print(result)
def processDir(img_folder, img_lang, file_title): lang = [img_lang] # if img_lang != 'en': # lang.append(img_lang) file_names = [ int(f.split(".")[0]) for f in os.listdir(img_folder) if os.path.isfile(os.path.join(img_folder, f)) ] file_names.sort() content = "" reader = easyocr.Reader(lang) for f in file_names: file = img_folder + "/" + str(f) + ".jpeg" im = PILIMG.open(file) result = reader.readtext(file) print(file, img_lang) st = "" for i in result: st += i[1] + " " content += st + " " print(content) res_img = draw_boxes(im, result) res_file_path = img_folder + "/" + str(f) + "_res.jpeg" res_img.save(res_file_path) print(content) audio_file = convertAudio(content, img_lang, file_title) return content, audio_file
def getNumberPlateSting(image): img = cv2.imread(image) gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) bfilter = cv2.bilateralFilter(gray, 11, 17, 17) # noise removal edged = cv2.Canny(bfilter, 30, 200) # edge detection keypoints = cv2.findContours(edged.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) contours = imutils.grab_contours(keypoints) contours = sorted(contours, key=cv2.contourArea, reverse=True)[:10] location = None for contour in contours: approx = cv2.approxPolyDP(contour, 10, True) if (len(approx) == 4): location = approx break mask = np.zeros(gray.shape, np.uint8) new_image = cv2.drawContours(mask, [location], 0, 255, -1) new_image = cv2.bitwise_and(img, img, mask=mask) (x, y) = np.where(mask == 255) (x1, y1) = (np.min(x), np.min(y)) (x2, y2) = (np.max(x), np.max(y)) cropped_image = gray[x1:x2 + 1, y1:y2 + 1] reader = easyocr.Reader(['en']) result = reader.readtext(cropped_image) text = result[0][-2] return text
def Easy_OCR(img): # Text Extraction reader = easyocr.Reader(['id', 'en'], gpu=False) result = reader.readtext(img, detail=1, paragraph=False) txt_list = reader.readtext(img, detail=0, paragraph=False) # Doing OCR. Get bounding boxes. # bounds = reader.readtext(image_path, detail=1) #detail=1 argument will only give text with its position in an image # bounds # Draw bounding boxes # loop over the results for (bbox, text, prob) in result: # display the OCR'd text and associated probability print("[INFO] {:.4f}: {}".format(prob, text)) # unpack the bounding box (tl, tr, br, bl) = bbox tl = (int(tl[0]), int(tl[1])) tr = (int(tr[0]), int(tr[1])) br = (int(br[0]), int(br[1])) bl = (int(bl[0]), int(bl[1])) # cleanup the text and draw the box surrounding the text along # with the OCR'd text itself text = cleanup_text(text) cv2.rectangle(img, tl, br, (0, 255, 0), 2) cv2.putText(img, text, (tl[0], tl[1] - 10), \ cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 255, 0), 2) # show the output image show_wait_destroy("Image", img) return txt_list
def maybe_find_license_plate_text(raw_img, grey_img, contours): # find rectangles locations = [] for contour in contours: approx = cv2.approxPolyDP(contour, 10, True) # approx polygon in contour if len(approx) == 4: locations.append(approx) # found a rectangle location # print(locations) for location in locations: mask = np.zeros(grey_img.shape, np.uint8) new_img = cv2.drawContours(mask, [location], 0, 255, -1) new_img = cv2.bitwise_and(raw_img, raw_img, mask=mask) cropped_img = crop_image(grey_img, mask) # instantiate easyocr reader with english language reader = easyocr.Reader(['en']) # pass cropped image to reader result = reader.readtext(cropped_img) try: text = result[-1][-2] if len(text) >= MIN_PLATE_NUM_LEN: # valid license plate text found return text, location else: raise IndexError() except IndexError: pass raise Exception("No license plate found :(")
def main(): # classifier to detect math vs non-math text clf = Classifier() reader = easyocr.Reader(['en']) root = Path(f"./image-dataset/{sys.argv[1]}/") for image in root.glob("*.jpg"): print("Processing", image) boxes_data = reader.readtext(str(image), batch_size=16, beamWidth=1, link_threshold=0.1, low_text=0.4, height_ths=0.9, ycenter_ths=0.9) boxes_data = remove_non_math(clf, boxes_data) boxes = non_max_suppression_fast(np.array([ (l[0][0], l[0][1], l[2][0], l[2][1]) for l, _, _ in boxes_data ]), overlapThresh=0.1) boxes = [(x1, y1, (x2 - x1), (y2 - y1)) for x1, y1, x2, y2 in boxes] plot(image, boxes) # printing table = [] for _, text, confidence in boxes_data: table.append([text, 1]) f = open(image.parent / "bb" / (image.parts[-1].replace("jpg", "txt")), "w") print(tabulate(table, headers=["Text", "Label"]), file=f)
def text_EASYOCR(IMAGE_PATH): reader = easyocr.Reader(['en', 'ru'], gpu=True) result = reader.readtext(IMAGE_PATH) text = '' for detection in result: text = text + '\n' + detection[1] return text
def car_recogniser(our_img): car_image = our_img trans = transforms.Compose([ transforms.Resize((400, 400)), transforms.RandomRotation(15), transforms.RandomHorizontalFlip(), transforms.ToTensor(), transforms.RandomErasing(inplace=True), transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)) ]) # preprocessing for prediction image input = trans(car_image) input = input.view(1, 3, 400, 400) output = car_model(input) prediction = int(torch.max(output.data, 1)[1].numpy()) # return prediction label predicted_val = ([value for value in labels_dict.values()][prediction]) st.text("Detected vehicle model: ") predicted_val #converting PIL object into numpy array for ocr new_array = np.array(car_image) reader = easyocr.Reader(['en'], gpu=False) bounds = reader.readtext(new_array, detail=0) st.text("Detected license plate number: ") for x in bounds: x
def __init__(self): self.mongo_uri = get_project_settings().get('MONGO_URI') self.mongo_db = get_project_settings().get('MONGO_DATABASE') self.collection = 'one_petro' self.client = pymongo.MongoClient(self.mongo_uri) self.db = self.client[self.mongo_db] self.reader = easyocr.Reader(['en'], gpu=False)
def main(): # Ornek olarak bir gorseli alip grayscale'a donusturuyoruz. img = cv2.imread('assets/image1.jpg') gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # plt.imshow(cv2.cvtColor(gray, cv2.COLOR_BGR2RGB)) b_filter = cv2.bilateralFilter(gray, 11, 17, 17) # Noise dusuruyoruz. edged = cv2.Canny(b_filter, 30, 200) # Kenar tespiti. # gorsellerde countour(sekil) tespiti. keypoints = cv2.findContours(edged.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) contours = imutils.grab_contours(keypoints) contours = sorted(contours, key=cv2.contourArea, reverse=True)[:10] location = None for contour in contours: # kare sekillerinin tespiti icin ideal parametre verilir. approx = cv2.approxPolyDP(contour, 10, True) if len(approx) == 4: location = approx break mask = np.zeros(gray.shape, np.uint8) new_image = cv2.drawContours(mask, [location], 0, 255, -1) new_image = cv2.bitwise_and(img, img, mask=mask) (x, y) = np.where(mask == 255) (x1, y1) = (np.min(x), np.min(y)) (x2, y2) = (np.max(x), np.max(y)) cropped_image = gray[x1:x2 + 1, y1:y2 + 1] # OCR teknolojisi kullanarak kirpilan gorseldeki plaka metnini kaziyoruz. reader = easyocr.Reader(['en']) result = reader.readtext(cropped_image) print(result) # OCR SONU # maskeleme ve kirpma islemi bitince islenen goruntuyu goster. # kazinan plaka numarasi. text = result[0][-2] font = cv2.FONT_HERSHEY_SIMPLEX res = cv2.putText(img, text=text, org=(approx[0][0][0], approx[1][0][1] + 75), fontFace=font, fontScale=1, color=(0, 255, 0), thickness=2) res = cv2.rectangle(img, tuple(approx[0][0]), tuple(approx[2][0]), (0, 255, 0), 3) plt.imshow(cv2.cvtColor(res, cv2.COLOR_BGR2RGB)) plt.show()
def car_recogniser_exit(our_img): # Establishing the connection conn = psycopg2.connect(database="vehicle", user='******', password='******', host='127.0.0.1', port='5432') # Setting auto commit false conn.autocommit = True # Creating a cursor object using the cursor() method cursor = conn.cursor() car_image = our_img trans = transforms.Compose([ transforms.Resize((400, 400)), transforms.RandomRotation(15), transforms.RandomHorizontalFlip(), transforms.ToTensor(), transforms.RandomErasing(inplace=True), transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)) ]) # preprocessing for prediction image input = trans(car_image) input = input.view(1, 3, 400, 400) output = car_model(input) prediction = int(torch.max(output.data, 1)[1].numpy()) # return prediction label predicted_val = ([value for value in labels_dict.values()][prediction]) # comparing algo st.text("Detected vehicle model: ") predicted_val # converting PIL object into numpy array for ocr new_array = np.array(car_image) reader = easyocr.Reader(['en'], gpu=False) bounds = reader.readtext(new_array, detail=0) st.text("Detected license plate number: ") num_plate = ' '.join([str(elem) for elem in bounds]) num_plate ext_time = current_time() st.text("The vehicle enter the parking at:") ext_time time_exit = ext_time.strftime("%Y/%m/%d, %H:%M:%S") day_exit = ext_time.strftime("%Y %m %d") sql = """INSERT INTO vehicle_data_exit(vehicle_brand, plate_number, exit_time, exit_date) VALUES(%s,%s,%s,%s)""" record_to_enter = (predicted_val, num_plate, time_exit, day_exit) cursor.execute(sql, record_to_enter) conn.commit() cursor.close() conn.close()
def __init__(*args, **kwargs): self = args[0] self.ocr = easyocr.Reader(['en']) # need to run only once to load model into memory) self.debug = False for arg in kwargs: if arg == "debug": self.debug = kwargs[arg]
def __init__(self, imageName, debug=False): self.reader = easyocr.Reader(['en'], gpu=False) self.color = 'yellow' self.width = 2 self.img = PIL.Image self.input_image = imageName self.bounds = None self.debug = debug
def ocr(img_path): """ :param img_path: The path to the YouTube mobile screenshot :return: OCR data including location coordinates and text """ reader = easyocr.Reader(['en']) return reader.readtext(img_path)
def __init__(self, langs, gpu=False, model_dir=None): # WARNING: This is a temporary usage. # Ideal thing to do would be just using the recognizer model alone. self.reader = easyocr.Reader(langs, gpu=gpu, model_storage_directory=model_dir) self.detector = None self.device = 'cuda' if gpu else 'cpu'
def __init__(self, uuid=None): if uuid == None: self.d = u2.connect() else: self.d = u2.connect(uuid) d_width, _ = self.d.window_size() self.d_width_center = d_width / 2 self.reader = easyocr.Reader(['ch_sim', 'en'], gpu=False)