import cv2
import numpy as np
import imutils
import pytesseract
import openpyxl

# Load the image and convert to grayscale
img = cv2.imread('D:/Mssmjktasrdtcc/Projeler/NAME_1/Endeks Kontrol/Data_Youtube/Videos/18.04.2023/frames/frame_0110.png')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# Increase contrast
alpha = 1.8
beta = 1
contrast = cv2.convertScaleAbs(gray, alpha=alpha, beta=beta)

# Apply filter and find egdes
filter = cv2.bilateralFilter(gray,11,30,2)
thresh = cv2.Canny(filter,30,200)

# Apply Hough transform to detect horizontal line segments
lines_horizontal = cv2.HoughLinesP(thresh, rho=1, theta=np.pi/180, threshold=100, minLineLength=100, maxLineGap=4)

# Filter out horizontal lines shorter than 200 pixels
lines_horizontal = [line for line in lines_horizontal if abs(line[0][0] - line[0][2]) > 200]

# Stretch remaining horizontal lines across the image
for i in range(len(lines_horizontal)):
    if i == 0:
        lines_horizontal[i][0][0] = 0
        lines_horizontal[i][0][2] = img.shape[1]
    else:
        lines_horizontal[i][0][0] = lines_horizontal[0][0][0]
        lines_horizontal[i][0][2] = lines_horizontal[0][0][2]

# Apply Hough transform to detect vertical line segments
lines_vertical = cv2.HoughLinesP(thresh, rho=1, theta=np.pi/180, threshold=100, minLineLength=100, maxLineGap=4)

# Filter out vertical lines shorter than 200 pixels and those outside the range of the green and red lines
lines_vertical = [line for line in lines_vertical if abs(line[0][1] - line[0][3]) > 200