Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EasyOCR not working in when packed into .exe #1359

Open
TLiley2 opened this issue Jan 5, 2025 · 0 comments
Open

EasyOCR not working in when packed into .exe #1359

TLiley2 opened this issue Jan 5, 2025 · 0 comments

Comments

@TLiley2
Copy link

TLiley2 commented Jan 5, 2025

This code for some reason only works when used in a python ide / anaconda cmd, but not when packed into a .exe. The .exe works on my coding laptop, but when tested on my other non-coding laptop (that doesn't have python installed) it doesnt work I want to know why that is? Am I not adding the necessary "additional files" or not adding a necessary "hidden import"? Can someone help
test.json

import easyocr
import os
import sys
import sys

import warnings
warnings.filterwarnings("ignore", module="easyocr")
os.environ["PYTHONWARNINGS"] = "ignore"

# sys.stderr = open(os.devnull, 'w') # Prevent printing warnings
b=[]
if getattr(sys, 'frozen', False):
    script_dir = os.path.dirname(os.path.abspath(sys.argv[0]))
else:
    script_dir = os.path.dirname(os.path.abspath(__file__))
reader = easyocr.Reader(['ja'], user_network_directory=script_dir, recog_network="japanese_g2", gpu=True)

import cv2

# Load image
path_set = os.path.join(script_dir, "swstella_system_cg1.jpg")
img = cv2.imread(path_set)
if img is None:
    print("Image not found at path")

# Increase contrast
contrast = cv2.convertScaleAbs(img, alpha=1.5, beta=1)

# Greyscale
gray = cv2.cvtColor(contrast, cv2.COLOR_BGR2GRAY)

# Denoise the image with Gaussian Blur
#blurred = cv2.GaussianBlur(gray, (3, 3), 0)

# Threshold the image (binary)
_, threshold_img = cv2.threshold(gray, 128, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)

# Calculate the percentage of black pixels
import numpy as np
black_pixels = np.sum(threshold_img == 0)
total_pixels = threshold_img.size
black_ratio = black_pixels / total_pixels

# Invert the image if black pixels dominate
if black_ratio > 0.75:
    processed_img = cv2.bitwise_not(threshold_img)
else:
    processed_img = threshold_img

# Perform OCR on the processed image
result = reader.readtext(processed_img, text_threshold=0.7, low_text=0.5)
print(str(result))

if getattr(sys, 'frozen', False):
    script_dir = os.path.dirname(os.path.abspath(sys.argv[0]))
else:
    script_dir = os.path.dirname(os.path.abspath(__file__))
path_set = os.path.join(script_dir, "file.txt")
print(path_set)
try:
    with open(path_set, "wb") as file:  # Open in binary mode
        file.write(str(result).encode("utf-8"))
    print(f"File written successfully to {path_set}")
except Exception as e:
    print(f"An error occurred: {e}")

# height, width = img.shape[:2]
# aspect_ratio = height / width
# new_height = int(800 * aspect_ratio)
# cv2.namedWindow("Image", cv2.WINDOW_NORMAL)  
# cv2.resizeWindow("Image", 800, new_height)         
# cv2.imshow("Image", processed_img)                   
# cv2.waitKey(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant