Lots of updates

This commit is contained in:
Mike Abbott 2023-02-16 17:27:06 -07:00
parent d3edd36d8e
commit 9e55c7c901
5 changed files with 119 additions and 84 deletions

View File

@ -1,12 +1,4 @@
FROM debian:bullseye-slim
# RUN apk add --no-cache make automake gcc g++ subversion python3-dev
# RUN apk add --no-cache python3 py3-pip
RUN apt-get update && apt-get install -y python3 python3-pip
RUN pip3 install opencv-python-headless
# RUN pip3 install --upgrade pip setuptools wheel
# RUN pip3 install opencv-python
# RUN apt-get update && apt-get install ffmpeg libsm6 libxext6 -y
RUN apt-get update && apt-get install -y git
RUN pip3 install aiohttp requests
RUN apt-get update && apt-get install -y ffmpeg
RUN apt-get update && apt-get install -y python3 python3-pip git ffmpeg
RUN pip3 install opencv-python-headless matplotlib aiohttp requests

View File

@ -1,7 +1,12 @@
{
"name": "rubedo",
"build": {
"dockerfile":"Dockerfile"
"dockerfile":"Dockerfile",
"args": {
"USERNAME": "vscode",
"BUILDKIT_INLINE_CACHE": "0"
}
},
"extensions": ["ms-python.python", "076923.python-image-preview"]
}

4
.gitignore vendored
View File

@ -154,4 +154,6 @@ cython_debug/
frame_data/
.idea/
*.gif
*.mp4
*.mp4
*.png
*.avi

View File

@ -29,13 +29,14 @@ def move_relative(x: float = None, y: float = None, z: float = None, f: float =
""")
# async def send_gcode(gcode: str):
# async with aiohttp.ClientSession() as session:
# json_data = {
# "script": gcode
# }
# async with session.post(HOST + GCODE_ENDPOINT, json=json_data) as resp:
# return await resp.json()
async def send_gcode(gcode: str):
async with aiohttp.ClientSession() as session:
json_data = {
"script": gcode
}
async with session.post(HOST + GCODE_ENDPOINT, json=json_data) as resp:
return await resp.json()
def send_gcode(gcode: str):
json_data = {

165
main.py
View File

@ -1,87 +1,122 @@
import cv2
import numpy as np
from glob import glob
# result = cv2.imread("2023-01-16-213226.jpg")
ranking = []
for video_file in sorted(glob("sample_data2/*")):
video_data = cv2.VideoCapture(video_file)
out = cv2.VideoWriter("out.avi", cv2.VideoWriter_fourcc('M','J','P','G'), 30, (400,400))
mid_y = 720//2
mid_x = 1280//2
from collections.abc import Iterable
import matplotlib.pyplot as plt
from pathlib import Path
frame_index = 0
def brightest_average(pixel_values: np.ndarray):
brightest_pixels = np.argsort(pixel_values)[-3:]
line_brightest_x = np.average(brightest_pixels)
return line_brightest_x
video_std = []
while video_data.isOpened():
ret, frame = video_data.read()
if not ret:
break
frame = frame[mid_y-100:mid_y+100, mid_x+100:mid_x+300]
lowerb = np.array([0, 0, 120])
upperb = np.array([255, 255, 255])
red_line = cv2.inRange(frame, lowerb, upperb)
def weighted_average(pixel_values: np.ndarray):
normalized_values = pixel_values / 255
adjusted_values = normalized_values
x_values = np.arange(pixel_values.size)
return np.average(x_values, weights=pixel_values)
masked_video = cv2.bitwise_and(frame,frame,mask = red_line)
# cv2.imwrite("test.png", masked_video)
# exit()
# out.write(masked_video)
gray = cv2.cvtColor(masked_video, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray, (3, 3), 0)
gray = cv2.GaussianBlur(gray, (3, 3), 0)
gray = cv2.GaussianBlur(gray, (11, 11), 0)
gray = cv2.GaussianBlur(gray, (11, 11), 0)
frame_brightest_x = []
def first_non_zero(pixel_values: np.ndarray):
return np.nonzero(pixel_values)[0][0]
for line in gray:
# find the 4 brightest pixels
if line.max() > 0:
brightest_pixels = np.argsort(line)[-4:]
line_brightest_x = np.average(brightest_pixels)
frame_brightest_x.append(line_brightest_x)
gray = cv2.cvtColor(gray, cv2.COLOR_GRAY2BGR)
def count_non_zero(pixel_values: np.ndarray):
return np.count_nonzero(pixel_values)
if len(frame_brightest_x) > 0:
def compute_x_value(pixel_values: np.ndarray):
algorithms = {
"brightest_avg": brightest_average,
"weighted_avg": weighted_average,
"first_non_zero": first_non_zero,
"count_non_zero": count_non_zero,
}
return algorithms["brightest_avg"](pixel_values)
# return algorithms["count_non_zero"](pixel_values)
# return algorithms["first_non_zero"](pixel_values)
# return algorithms["weighted_avg"](pixel_values)
def graph_frame(pixel_values: np.ndarray, output_file: str):
return
plt.figure()
plt.plot(pixel_values)
plt.ylim([0, 200])
plt.savefig(output_file)
plt.close()
def compute_score_for_frame(x_values: Iterable):
return np.std(x_values)
def main():
ranking = []
for video_file in sorted(glob("sample_data2/*")):
video_data = cv2.VideoCapture(video_file)
out = cv2.VideoWriter("out.avi", cv2.VideoWriter_fourcc('M','J','P','G'), 30, (400,400))
mid_y = 720//2 + 15
mid_x = 1280//2 + 30
frame_index = 0
video_std = []
while video_data.isOpened():
ret, frame = video_data.read()
if not ret:
break
frame = frame[mid_y-50:mid_y+50, mid_x+100:mid_x+300]
lowerb = np.array([0, 0, 120])
upperb = np.array([255, 255, 255])
red_line = cv2.inRange(frame, lowerb, upperb)
masked_video = cv2.bitwise_and(frame,frame,mask = red_line)
# cv2.imwrite("test.png", masked_video)
# exit()
# out.write(masked_video)
gray = cv2.cvtColor(masked_video, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray, (3, 3), 0)
gray = cv2.GaussianBlur(gray, (3, 3), 0)
gray = cv2.GaussianBlur(gray, (11, 11), 0)
gray = cv2.GaussianBlur(gray, (11, 11), 0)
laser_x_values = []
for line in gray:
# find the 4 brightest pixels
if line.max() > 0:
laser_x_val = compute_x_value(line)
laser_x_values.append(laser_x_val)
graph_frame(laser_x_values, f"graphs/{Path(video_file).stem}-{frame_index}.png")
# gray = cv2.cvtColor(gray, cv2.COLOR_GRAY2BGR)
# out.write(gray)
# cv2.imwrite(f"frame_data/{frame_index}.png", gray)
cv2.imwrite(f"frame_data/{Path(video_file).stem}-{frame_index}.png", gray)
frame_std = np.std(frame_brightest_x)
frame_score = compute_score_for_frame(laser_x_values)
# print(frame_index, frame_std)
video_std.append(frame_std)
video_std.append(frame_score)
frame_index += 1
# red_line = cv2.cvtColor(red_line, cv2.COLOR_GRAY2BGR)
# out.write(red_line)
# out.release()
print(video_file, np.std(video_std))
# exit()
# out.release()
print(np.std(video_std))
# if True:
# # if "line7.mp4" in video_file:
# break
ranking.append((video_file, np.std(video_std)))
ranking.append((video_file, np.std(video_std)))
print('\nSCORES\n')
print('\nSCORES\n')
[ print(x) for x in sorted(ranking, key=lambda x: x[1])]
[ print(x) for x in sorted(ranking, key=lambda x: x[1])]
# cv2.imwrite("test.png", frame)
# line starts on frame 12
# line ends on frame 32
# line starts on frame 41
# line ends on 61
# line starts on 70
# line ends on 90
# line starts on 99
# line ends on 119
if __name__=="__main__":
main()