c:\Users\shivpatel\Downloads\angle graph code
unknown
python
2 years ago
3.6 kB
5
Indexable
import cv2
import numpy as np
from concurrent.futures import ThreadPoolExecutor
import time
import pandas as pd
import matplotlib.pyplot as plt
def process_frame(frame):
frame = cv2.resize(frame,(500,500))
#frame = frame[500:800,200:900]
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (5, 5), 0)
smoothed_image = cv2.bilateralFilter(blurred, d=5, sigmaColor=130, sigmaSpace=130)
equ = cv2.equalizeHist(smoothed_image)
return equ
def detect_edges(image):
return cv2.Canny(image, 95, 70)
def detect_lines(edges):
lines = cv2.HoughLinesP(edges, 1, np.pi/180, threshold=70, minLineLength=35, maxLineGap=30)
return lines
def angle_average(angles_in_frame):
avg_angle = np.mean(angles_in_frame)
return avg_angle
# Initialize the camera capture
cap = cv2.VideoCapture(0)
prev_frame = 0
line_data = []
if not cap.isOpened():
print("Error: Could not access the camera.")
else:
with ThreadPoolExecutor() as executor:
while True:
ret, frame = cap.read()
if not ret:
break
equ = executor.submit(process_frame, frame).result()
edges = executor.submit(detect_edges, equ).result()
lines = executor.submit(detect_lines, edges).result()
line_image = frame.copy()
angles_in_frame = []
if lines is not None:
for line in lines:
x1, y1, x2, y2 = line[0]
cv2.line(line_image, (x1, y1), (x2, y2), (0, 0, 255), 2)
angle = np.arctan2(y2 - y1, x2 - x1)
angle_degrees = np.degrees(angle)
if 40>angle_degrees>60:
continue # Skip lines with an angle of 0
if angle_degrees < 0:
angle_degrees = angle_degrees + 90
elif angle_degrees > 0:
angle_degrees = 90 - angle_degrees
angle_degrees = round(angle_degrees)
cv2.putText(line_image, f'{angle_degrees:.2f} deg', (x1, y1), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 3)
line_info = [int(x1), int(y1), int(x2), int(y2), float(angle_degrees)]
line_data.append(line_info)
angles_in_frame.append(angle_degrees)
avg_angle = angle_average(angles_in_frame)
cv2.putText(line_image, f'Avg Angle: {avg_angle:.2f} deg', (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
next_frame = cv2.getTickCount()
fps = cv2.getTickFrequency() / (next_frame - prev_frame)
prev_frame = next_frame
cv2.putText(line_image, f'FPS: {fps:.2f}', (10, 60), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
if cv2.waitKey(1) & 0xFF == 27: # Press 'Esc' to exit the loop
break
cv2.imshow("canny",edges)
cv2.imshow('Processed Image', line_image)
if cv2.waitKey(1) & 0xFF == 27:
break
# Convert line_data to a Pandas DataFrame
columns = ['x1', 'y1', 'x2', 'y2', 'angle_degrees']
df = pd.DataFrame(line_data, columns=columns)
# Save DataFrame to Excel file
excel_file_path = 'line_data.xlsx'
df.to_excel(excel_file_path, index=False)
# Plotting the scatter plot
plt.scatter(range(len(df)), df['angle_degrees'])
plt.xlabel('Frame')
plt.ylabel('Angle (degrees)')
plt.title('Scatter Plot of Angle Over Time')
plt.show()
# Release the camera and close all windows
cap.release()
cv2.destroyAllWindows()Editor is loading...