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 FROM debian:bullseye-slim
# RUN apk add --no-cache make automake gcc g++ subversion python3-dev RUN apt-get update && apt-get install -y python3 python3-pip git ffmpeg
# RUN apk add --no-cache python3 py3-pip RUN pip3 install opencv-python-headless matplotlib aiohttp requests
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

View File

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

2
.gitignore vendored
View File

@ -155,3 +155,5 @@ frame_data/
.idea/ .idea/
*.gif *.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 def send_gcode(gcode: str):
# async with aiohttp.ClientSession() as session: async with aiohttp.ClientSession() as session:
# json_data = { json_data = {
# "script": gcode "script": gcode
# } }
# async with session.post(HOST + GCODE_ENDPOINT, json=json_data) as resp: async with session.post(HOST + GCODE_ENDPOINT, json=json_data) as resp:
# return await resp.json() return await resp.json()
def send_gcode(gcode: str): def send_gcode(gcode: str):
json_data = { json_data = {

105
main.py
View File

@ -1,18 +1,67 @@
import cv2 import cv2
import numpy as np import numpy as np
from glob import glob from glob import glob
from collections.abc import Iterable
import matplotlib.pyplot as plt
from pathlib import Path
# result = cv2.imread("2023-01-16-213226.jpg")
ranking = []
for video_file in sorted(glob("sample_data2/*")): 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
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)
def first_non_zero(pixel_values: np.ndarray):
return np.nonzero(pixel_values)[0][0]
def count_non_zero(pixel_values: np.ndarray):
return np.count_nonzero(pixel_values)
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) video_data = cv2.VideoCapture(video_file)
out = cv2.VideoWriter("out.avi", cv2.VideoWriter_fourcc('M','J','P','G'), 30, (400,400)) out = cv2.VideoWriter("out.avi", cv2.VideoWriter_fourcc('M','J','P','G'), 30, (400,400))
mid_y = 720//2 mid_y = 720//2 + 15
mid_x = 1280//2 mid_x = 1280//2 + 30
frame_index = 0 frame_index = 0
@ -21,7 +70,7 @@ for video_file in sorted(glob("sample_data2/*")):
ret, frame = video_data.read() ret, frame = video_data.read()
if not ret: if not ret:
break break
frame = frame[mid_y-100:mid_y+100, mid_x+100:mid_x+300] frame = frame[mid_y-50:mid_y+50, mid_x+100:mid_x+300]
lowerb = np.array([0, 0, 120]) lowerb = np.array([0, 0, 120])
upperb = np.array([255, 255, 255]) upperb = np.array([255, 255, 255])
@ -38,50 +87,36 @@ for video_file in sorted(glob("sample_data2/*")):
gray = cv2.GaussianBlur(gray, (11, 11), 0) gray = cv2.GaussianBlur(gray, (11, 11), 0)
gray = cv2.GaussianBlur(gray, (11, 11), 0) gray = cv2.GaussianBlur(gray, (11, 11), 0)
frame_brightest_x = [] laser_x_values = []
for line in gray: for line in gray:
# find the 4 brightest pixels # find the 4 brightest pixels
if line.max() > 0: if line.max() > 0:
brightest_pixels = np.argsort(line)[-4:] laser_x_val = compute_x_value(line)
line_brightest_x = np.average(brightest_pixels) laser_x_values.append(laser_x_val)
frame_brightest_x.append(line_brightest_x)
gray = cv2.cvtColor(gray, cv2.COLOR_GRAY2BGR) graph_frame(laser_x_values, f"graphs/{Path(video_file).stem}-{frame_index}.png")
if len(frame_brightest_x) > 0: # gray = cv2.cvtColor(gray, cv2.COLOR_GRAY2BGR)
# out.write(gray) # 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) # print(frame_index, frame_std)
video_std.append(frame_std) video_std.append(frame_score)
frame_index += 1 frame_index += 1
# red_line = cv2.cvtColor(red_line, cv2.COLOR_GRAY2BGR) # red_line = cv2.cvtColor(red_line, cv2.COLOR_GRAY2BGR)
# out.write(red_line) # out.write(red_line)
# exit()
# out.release() # out.release()
print(video_file, np.std(video_std)) 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])]
if __name__=="__main__":
main()
# 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