When I run same code in power automate desktop, Gives an error.
import os
import cv2
import numpy as np
def correct_image_angle(image_path😞
# Read the image
image = cv2.imread(image_path)
# Convert to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Detect edges using Canny edge detector
edges = cv2.Canny(gray, 50, 150, apertureSize=3)
# Find lines using Hough Line Transform
lines = cv2.HoughLines(edges, 1, np.pi / 180, 200)
# Calculate angle of rotation
angle = 0
if lines is not None and len(lines) > 0:
angles = []
for line in lines:
for rho, theta in line:
a = np.cos(theta)
b = np.sin(theta)
x0 = a * rho
y0 = b * rho
x1 = int(x0 + 1000 * (-b))
y1 = int(y0 + 1000 * (a))
x2 = int(x0 - 1000 * (-b))
y2 = int(y0 - 1000 * (a))
angles.append(np.arctan2(y2 - y1, x2 - x1) * 180.0 / np.pi)
angle = np.mean(angles)
# Rotate the image
rotated_image = cv2.rotate(image, cv2.ROTATE_90_CLOCKWISE if angle > 45 else cv2.ROTATE_90_COUNTERCLOCKWISE)
return rotated_image
def process_images(input_folder, output_folder😞
# Create the output folder if it doesn't exist
if not os.path.exists(output_folder😞
os.makedirs(output_folder)
# Process each image in the input folder
for filename in os.listdir(input_folder😞
if filename.endswith(('.jpg', '.jpeg', '.png')):
input_path = os.path.join(input_folder, filename)
output_path = os.path.join(output_folder, filename)
# Correct the image angle
corrected_image = correct_image_angle(input_path)
# Save the corrected image
cv2.imwrite(output_path, corrected_image)
print(f"Processed {filename}")
# Example usage:
input_folder = r'C:\Users\sanket.shinde\Downloads\Sample pics'
output_folder = r'C:\Users\sanket.shinde\Downloads\Output'
process_images(input_folder, output_folder)