mirror of
https://github.com/furrysalamander/rubedo.git
synced 2025-11-04 15:49:40 +08:00
Lots of updates
This commit is contained in:
parent
d3edd36d8e
commit
9e55c7c901
@ -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
|
|
||||||
|
|||||||
@ -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
2
.gitignore
vendored
@ -155,3 +155,5 @@ frame_data/
|
|||||||
.idea/
|
.idea/
|
||||||
*.gif
|
*.gif
|
||||||
*.mp4
|
*.mp4
|
||||||
|
*.png
|
||||||
|
*.avi
|
||||||
@ -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 = {
|
||||||
|
|||||||
161
main.py
161
main.py
@ -1,87 +1,122 @@
|
|||||||
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
|
||||||
# result = cv2.imread("2023-01-16-213226.jpg")
|
import matplotlib.pyplot as plt
|
||||||
ranking = []
|
from pathlib import Path
|
||||||
|
|
||||||
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
|
|
||||||
|
|
||||||
|
|
||||||
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])
|
def weighted_average(pixel_values: np.ndarray):
|
||||||
upperb = np.array([255, 255, 255])
|
normalized_values = pixel_values / 255
|
||||||
red_line = cv2.inRange(frame, lowerb, upperb)
|
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)
|
def first_non_zero(pixel_values: np.ndarray):
|
||||||
gray = cv2.GaussianBlur(gray, (3, 3), 0)
|
return np.nonzero(pixel_values)[0][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 count_non_zero(pixel_values: np.ndarray):
|
||||||
|
return np.count_nonzero(pixel_values)
|
||||||
|
|
||||||
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 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)
|
||||||
|
|
||||||
if len(frame_brightest_x) > 0:
|
|
||||||
|
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)
|
# 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)
|
||||||
# out.release()
|
# exit()
|
||||||
print(video_file, np.std(video_std))
|
# out.release()
|
||||||
|
print(np.std(video_std))
|
||||||
|
|
||||||
# if True:
|
ranking.append((video_file, np.std(video_std)))
|
||||||
# # if "line7.mp4" in video_file:
|
|
||||||
# break
|
|
||||||
|
|
||||||
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
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user