Add up/down binding, fix OOR bug

This commit is contained in:
ABelliqueux 2024-09-11 12:08:42 +02:00
parent 0e49b446c6
commit 5aec1e0dac
2 changed files with 48 additions and 30 deletions

View File

@ -10,11 +10,13 @@ import sys
import subprocess import subprocess
import time import time
import tomllib import tomllib
# TODO : remove tkinter dep
# ~ import tkinter as tk
# ~ from tkinter import messagebox
import numpy as np import numpy as np
# TODO : find camera module version from image size
# https://www.raspberrypi.com/documentation/accessories/camera.html#hardware-specification
# v4l2-ctl --list-framesizes=YU12
# 2592 x 1944 == v1
# 3280 × 2464 == v2
running_from_folder = os.path.realpath(__file__) running_from_folder = os.path.realpath(__file__)
alphabet = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'] alphabet = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']
@ -265,6 +267,16 @@ def previous_frame(img_index):
return img_index, update_image(img_list, img_index) return img_index, update_image(img_list, img_index)
def last_frame(img_index):
img_index = len(img_list)-1
return img_index, update_image(img_list, img_index)
def first_frame(img_index):
img_index = 0
return img_index, update_image(img_list, img_index)
def clean_img_list(folder_path): def clean_img_list(folder_path):
# Check file in dict exists, else remove it # Check file in dict exists, else remove it
file_list = os.listdir(folder_path) file_list = os.listdir(folder_path)
@ -322,7 +334,7 @@ def remove_frame(img_list, img_index):
# ~ frame_path = os.path.realpath(frame_name) # ~ frame_path = os.path.realpath(frame_name)
frame_path = os.path.join(folder_path, frame_name) frame_path = os.path.join(folder_path, frame_name)
if not os.path.exists(frame_path): if not os.path.exists(frame_path):
return img_index, blank_image return img_list, img_index, blank_image
print(_("Removing {}").format(frame_path)) print(_("Removing {}").format(frame_path))
# trash file # trash file
send2trash(frame_path) send2trash(frame_path)
@ -336,24 +348,16 @@ def remove_frame(img_list, img_index):
# update index if possible # update index if possible
img_index = check_range(img_index, False) img_index = check_range(img_index, False)
# update display # update display
return img_index, update_image(img_list, img_index) return img_list, img_index, update_image(img_list, img_index)
else: else:
return 0, blank_image return img_list, 0, blank_image
def playback_animation(img_list, img_index): def playback_animation(img_list, img_index):
# save onionskin state # save onionskin state
if onionskin: if onionskin:
onionskin = False onionskin = False
onionskin_was_on = True onionskin_was_on = True
# ~ index = 0
# Play all frames
# ~ while index < len(img_list):
# ~ print(img_list[index])
# ~ img = update_image(img_list, index)
# ~ cv2.imshow("StopiCV", img)
# ~ time.sleep(.5)
# ~ index += 1
for file in img_list: for file in img_list:
img = update_image(img_list, img_list.index(file)) img = update_image(img_list, img_list.index(file))
print(str(img_list.index(file)) + " : " + file ) print(str(img_list.index(file)) + " : " + file )
@ -410,19 +414,14 @@ def export_animation(input_filename, export_filename):
def main(args): def main(args):
global onionskin, playback, loop_playback, playhead, index global onionskin, playback, loop_playback, playhead, index, img_list
if not testDevice(0): if not testDevice(0):
print(_("No camera device found. Exiting...")) print(_("No camera device found. Exiting..."))
return 1 return 1
cam = cv2.VideoCapture(0) cam = cv2.VideoCapture(0)
cam.set(cv2.CAP_PROP_FRAME_WIDTH, 1280) cam.set(cv2.CAP_PROP_FRAME_WIDTH, 1600)
cam.set(cv2.CAP_PROP_FRAME_HEIGHT, 720) cam.set(cv2.CAP_PROP_FRAME_HEIGHT, 900)
# ~ cv2.namedWindow("StopiCV", cv2.WINDOW_GUI_NORMAL)
# ~ cv2.setWindowProperty("StopiCV", cv2.WND_PROP_OPENGL, cv2.WINDOW_OPENGL)
# ~ cv2.setWindowProperty("StopiCV", cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)
# ~ cv2.setWindowProperty("StopiCV", cv2.WND_PROP_ASPECT_RATIO, cv2.WINDOW_KEEPRATIO)
frame = get_onionskin_frame(savepath, index) frame = get_onionskin_frame(savepath, index)
while True: while True:
@ -450,7 +449,11 @@ def main(args):
# Resize preview # Resize preview
overlay = cv2.resize(overlay, (project_settings['screen_w'], project_settings['screen_h'])) overlay = cv2.resize(overlay, (project_settings['screen_w'], project_settings['screen_h']))
# Apply onionskin # Apply onionskin
overlay = cv2.addWeighted(frame, 1.0, overlay, project_settings['onionskin_alpha_default'], 0) alpha = project_settings['onionskin_alpha_default']
beta = (1.0 - alpha)
overlay = cv2.addWeighted(frame, alpha, overlay, beta, 0)
# TODO : use v4l-ctl to flip frame
# v4l2-ctl -d /dev/video0 -c vertical_flip=1
# Flip preview (0 = vert; 1 = hor) # Flip preview (0 = vert; 1 = hor)
if project_settings['vflip'] or project_settings['hflip']: if project_settings['vflip'] or project_settings['hflip']:
if project_settings['vflip'] and project_settings['hflip']: if project_settings['vflip'] and project_settings['hflip']:
@ -473,6 +476,19 @@ def main(args):
if (k%256 == 111) or (k%256 == 47): if (k%256 == 111) or (k%256 == 47):
# Toggle onionskin # Toggle onionskin
onionskin = not onionskin onionskin = not onionskin
# Key up
elif (k%256 == 82) or (k%256 == 56):
if len(img_list):
if playback:
playback = False
index, frame = last_frame(index)
# Key down
elif (k%256 == 84) or (k%256 == 50):
if len(img_list):
if playback:
playback = False
index, frame = first_frame(index)
print(index)
# Key left # Key left
elif (k%256 == 81) or (k%256 == 52): elif (k%256 == 81) or (k%256 == 52):
# Displau previous frame # Displau previous frame
@ -488,19 +504,20 @@ def main(args):
index, frame = next_frame(index) index, frame = next_frame(index)
# Key e / keypad * # Key e / keypad *
elif (k%256 == 101) or (k%256 == 42): elif (k%256 == 101) or (k%256 == 42):
print("Export") print(_("Export"))
ffmpeg_process = export_animation(input_filename, export_filename) ffmpeg_process = export_animation(input_filename, export_filename)
# Key Return # Key Return
elif (k%256 == 13): elif (k%256 == 13):
print("Playback") print(_("Playback"))
print(img_list)
playhead = index playhead = index
loop_playback = True loop_playback = True
playback = not playback playback = not playback
# Key remove frame - backspace, del, numpad_minus # Key remove frame - backspace, del, numpad_minus
elif (k%256 == 8) or (k%256 == 45) or (k == 255): elif (k%256 == 8) or (k%256 == 45) or (k == 255):
# Remove frame # Remove frame
print("Remove frame") print(_("Remove frame"))
index, frame = remove_frame(img_list, index) img_list, index, frame = remove_frame(img_list, index)
# Quit app # Quit app
elif k%256 == 27: elif k%256 == 27:
# ESC pressed # ESC pressed
@ -519,7 +536,7 @@ def main(args):
img_path = os.path.join(savepath, img_name) img_path = os.path.join(savepath, img_name)
cv2.imwrite(img_path, og_frame) cv2.imwrite(img_path, og_frame)
print(_("File {} written.").format(img_path)) print(_("File {} written.").format(img_path))
if len(img_list) and (img_list[index] == 'A.-001.png'): if len(img_list) and (img_list[index] == '{letter}.-001.{ext}'.format(letter=project_letter, ext=project_settings['file_extension'])):
img_list[index] = img_name img_list[index] = img_name
else: else:
index += 1 index += 1

View File

@ -1,4 +1,5 @@
Send2Trash Send2Trash
opencv-python opencv-python
numpy numpy
tkinter pyserial
pillow