В этом коде у меня есть блок кода, который автоматически поворачивает документ, если он находится в неправильном положении, поэтому сценарий обнаружения текста работает правильно, но проблема, с которой я столкнулся, заключается в том, что изображение вращается неправильно, и я застрял, и я буду признателен за вашу помощь, ребята
import cv2
import numpy as np
from imutils.perspective import four_point_transform
import pytesseract
import tkinter as tk
from tkinter import filedialog
import os
# Load the selected image
image = cv2.imread(r'C:\\Users\\zigzong\\kiko\\flie.jpg')
pytesseract.pytesseract.tesseract_cmd = 'C:\\Program Files\\Tesseract-OCR\\tesseract.exe'
scale = 0.5
font = cv2.FONT_HERSHEY_SIMPLEX
def image_processing(image):
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
_, threshold = cv2.threshold(gray, 128, 255, cv2.THRESH_BINARY)
return threshold
def rotate_if_needed(image, contour):
# Get the minimum area bounding box for the contour
rect = cv2.minAreaRect(contour)
box = cv2.boxPoints(rect)
box = np.int0(box)
# Calculate the angle of rotation
angle = rect[-1]
# Adjust the angle to ensure the document is straight
if angle < -45:
angle += 90
# Expand the canvas size to prevent cutting off during rotation
(h, w) = image.shape[:2]
center = (w // 2, h // 2)
M = cv2.getRotationMatrix2D(center, angle, 1.0)
# Calculate the new bounding dimensions after rotation
cos = np.abs(M[0, 0])
sin = np.abs(M[0, 1])
new_w = int((h * sin) + (w * cos))
new_h = int((h * cos) + (w * sin))
# Adjust the rotation matrix to take into account translation
M[0, 2] += (new_w / 2) - center[0]
M[1, 2] += (new_h / 2) - center[1]
# Perform the rotation
rotated = cv2.warpAffine(image, M, (new_w, new_h), flags=cv2.INTER_CUBIC, borderMode=cv2.BORDER_REPLICATE)
return rotated
def scan_detection(image):
global document_contour
document_contour = np.array([[0, 0], [image.shape[1], 0], [image.shape[1], image.shape[0]], [0, image.shape[0]]])
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (5, 5), 0)
_, threshold = cv2.threshold(blur, 127, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
contours, _ = cv2.findContours(threshold, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
contours = sorted(contours, key=cv2.contourArea, reverse=True)
max_area = 0
for contour in contours:
area = cv2.contourArea(contour)
if area > 1000:
peri = cv2.arcLength(contour, True)
approx = cv2.approxPolyDP(contour, 0.015 * peri, True)
if area > max_area and len(approx) == 4:
document_contour = approx
max_area = area
# Rotate the image if the document is not straight
rotated_image = rotate_if_needed(image, document_contour)
return rotated_image
# Main process to display and process the image
if image is not None:
# Copy the original image
frame_copy = image.copy()
# Perform document scan detection and rotation
rotated_image = scan_detection(frame_copy)
# Display the original image
cv2.imshow("Input Image", cv2.resize(image, (int(scale * image.shape[1]), int(scale * image.shape[0]))))
# Apply four-point transform to get a top-down view of the document
warped = four_point_transform(rotated_image, document_contour.reshape(4, 2))
cv2.imshow("Warped", cv2.resize(warped, (int(scale * warped.shape[1]), int(scale * warped.shape[0]))))
# Process the warped image (thresholding)
processed = image_processing(warped)
processed = processed[10:processed.shape[0] - 10, 10:processed.shape[1] - 10]
cv2.imshow("Processed", cv2.resize(processed, (int(scale * processed.shape[1]), int(scale * processed.shape[0]))))
# Save the processed image
cv2.imwrite("output/scanned_image.jpg", processed)
# Perform OCR on the processed image
ocr_text = pytesseract.image_to_string(processed)
print("Extracted Text from Image:")
print(ocr_text)
# Create the output directory if it doesn't exist
output_dir = "output"
if not os.path.exists(output_dir):
os.makedirs(output_dir)
# Save OCR results to a file in
cv2.waitKey(0)
cv2.destroyAllWindows()
В этом коде у меня есть блок кода, который автоматически поворачивает документ, если он находится в неправильном положении, поэтому сценарий обнаружения текста работает правильно, но проблема, с которой я столкнулся, заключается в том, что изображение вращается неправильно, и я застрял, и я буду признателен за вашу помощь, ребята [code]import cv2 import numpy as np from imutils.perspective import four_point_transform import pytesseract import tkinter as tk from tkinter import filedialog import os
# Load the selected image image = cv2.imread(r'C:\\Users\\zigzong\\kiko\\flie.jpg')
def rotate_if_needed(image, contour): # Get the minimum area bounding box for the contour rect = cv2.minAreaRect(contour) box = cv2.boxPoints(rect) box = np.int0(box)
# Calculate the angle of rotation angle = rect[-1]
# Adjust the angle to ensure the document is straight if angle < -45: angle += 90
# Expand the canvas size to prevent cutting off during rotation (h, w) = image.shape[:2] center = (w // 2, h // 2) M = cv2.getRotationMatrix2D(center, angle, 1.0)
# Calculate the new bounding dimensions after rotation cos = np.abs(M[0, 0]) sin = np.abs(M[0, 1]) new_w = int((h * sin) + (w * cos)) new_h = int((h * cos) + (w * sin))
# Adjust the rotation matrix to take into account translation M[0, 2] += (new_w / 2) - center[0] M[1, 2] += (new_h / 2) - center[1]
# Perform the rotation rotated = cv2.warpAffine(image, M, (new_w, new_h), flags=cv2.INTER_CUBIC, borderMode=cv2.BORDER_REPLICATE)
return rotated
def scan_detection(image): global document_contour
max_area = 0 for contour in contours: area = cv2.contourArea(contour) if area > 1000: peri = cv2.arcLength(contour, True) approx = cv2.approxPolyDP(contour, 0.015 * peri, True) if area > max_area and len(approx) == 4: document_contour = approx max_area = area
# Rotate the image if the document is not straight rotated_image = rotate_if_needed(image, document_contour)
return rotated_image
# Main process to display and process the image if image is not None: # Copy the original image frame_copy = image.copy()
# Perform document scan detection and rotation rotated_image = scan_detection(frame_copy)
# Display the original image cv2.imshow("Input Image", cv2.resize(image, (int(scale * image.shape[1]), int(scale * image.shape[0]))))
# Apply four-point transform to get a top-down view of the document warped = four_point_transform(rotated_image, document_contour.reshape(4, 2)) cv2.imshow("Warped", cv2.resize(warped, (int(scale * warped.shape[1]), int(scale * warped.shape[0]))))
У меня есть блок кода на Python, который должен вращать документ, но не в правильном направлении. При применении поворота он вращается в неправильном направлении, и я не могу извлечь из него текст, если он непрямой >
def rotate_if_needed(image,...
В этом коде у меня есть блок кода, который автоматически поворачивает документ, если он находится в неправильном положении, поэтому сценарий обнаружения текста работает правильно, но проблема, с которой я столкнулся, заключается в том, что...
Я пытался распечатать PDF-документ из Java с помощью PDFRenderer и ICEpdf. В обоих случаях часть текста повернулась на 180 градусов, а изображения остались правильными. В PDFREndere вращается весь текст, а в ICEpdf — только некоторые строки. Есть...
I tried to print a PDF document from Java using PDFRenderer and ICEpdf. In both cases some of the text came out rotated in 180 degrees while the images stayed correct. With PDFREndere all the text is rotated and in ICEpdf only some of the lines. Any...
Я хочу, чтобы высота и ширина холста менялись при повороте изображения и работали соответствующим образом. Наложение также должно соответствовать повернутому холсту, и обрезка должна работать соответствующим образом.