WIP displaying letter on result movie

This commit is contained in:
Andrzej Preibisz 2023-01-30 23:10:34 +01:00
parent 9d77afb09c
commit 50edd3d3f9

41
main.py
View File

@ -3,7 +3,9 @@ from process_video import segment_video, classify
from io import StringIO from io import StringIO
import cv2 as cv import cv2 as cv
import tempfile import tempfile
import os
import numpy as np import numpy as np
from PIL import Image
import tensorflow as tf import tensorflow as tf
from crop_hand_skeleton import crop_hand from crop_hand_skeleton import crop_hand
from cvzone.HandTrackingModule import HandDetector from cvzone.HandTrackingModule import HandDetector
@ -26,15 +28,52 @@ if __name__ == "__main__":
tfile = tempfile.NamedTemporaryFile(delete=False) tfile = tempfile.NamedTemporaryFile(delete=False)
tfile.write(upload_movie.read()) tfile.write(upload_movie.read())
video_cap = cv.VideoCapture(tfile.name) video_cap = cv.VideoCapture(tfile.name)
result, num = segment_video(video_cap, fps=10) result, num, frames = segment_video(video_cap, fps=3)
st.write(f"Załadowano {num} klatek") st.write(f"Załadowano {num} klatek")
classifications = []
for img in result: for img in result:
img_skeleton = crop_hand(img, detector) img_skeleton = crop_hand(img, detector)
img2= cv.resize(img_skeleton,dsize=(224,224)) img2= cv.resize(img_skeleton,dsize=(224,224))
#breakpoint() #breakpoint()
img_np = np.asarray(img2) img_np = np.asarray(img2)
classification = classify(img_np[:,:,::-1], model) classification = classify(img_np[:,:,::-1], model)
classifications.append(classification)
st.image(img_skeleton[:,:,::-1]) st.image(img_skeleton[:,:,::-1])
st.write(classification) st.write(classification)
i = 0
last_letter = ''
text = ''
font = cv.FONT_HERSHEY_SIMPLEX
width, height, layers = result[0].shape
fourcc = cv.VideoWriter_fourcc(*'mp4v')
new_video_cap = cv.VideoCapture(tfile.name)
new_video = cv.VideoWriter("output_video.mp4", fourcc, 30.0, (width, height), 3)
print(f"VIDEO CAP {result[0].shape}")
while True:
ret, frame = new_video_cap.read()
if ret == False:
break
if i in frames:
print(i)
frame_index = frames.index(i)
letter = classifications[frame_index]
last_letter = letter
st.write(last_letter)
cv.putText(frame,
last_letter,
(50, 50),
font, 1,
(0, 255, 255),
2,
cv.LINE_4)
img_pil = Image.fromarray(img)
frame = np.array(img_pil)
img = cv.cvtColor(frame, cv.COLOR_BGR2RGB)
new_video.write(img)
i += 1
video_cap.release()
new_video_cap.release()
new_video.release()