Last active
February 13, 2018 05:25
-
-
Save soravux/14230c556d9924908959daf316a4570d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import atexit | |
import time | |
import subprocess | |
from io import BytesIO | |
import PIL.Image | |
import numpy as np | |
from matplotlib import pyplot as plt | |
# S'assurer que le processus enfant se fait bien tuer, mais pas trop méchamment | |
def cleanup(): | |
timeout_sec = 5 | |
if "process" in globals(): | |
process.terminate() | |
for s in range(10*timeout_sec): | |
if process.poll() == None: | |
time.sleep(0.1) | |
else: | |
break | |
else: | |
process.kill() | |
atexit.register(cleanup) | |
# Appeler FFMPEG | |
# Sortie #1: fichier `output.mkv` avec 25fps | |
# Sortie #2: ce script Python à 1fps | |
cmd = ('ffmpeg -loglevel panic -y -f v4l2 -framerate 25 -video_size 640x480 -i /dev/video0 ' | |
'-c:v libx265 -crf 28 -c:a aac -b:a 128k output.mkv ' | |
'-vf "fps=1" -f rawvideo -pix_fmt rgb24 - ') | |
process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, | |
bufsize=10**8, shell=True) | |
while True: | |
# Lire l'image du pipe | |
raw = process.stdout.read(640*480*3) | |
if raw is not None: | |
# Interpréter les octets | |
im = np.fromstring(raw, dtype='uint8').reshape((480,640,3)) | |
# Afficher juste pour le plaisir | |
plt.clf() | |
plt.imshow(im) | |
plt.show(block=False) | |
plt.pause(0.01) | |
# Convertir en JPG pour envoi au serveur | |
buf = BytesIO() | |
PIL.Image.fromarray(im).save(buf, 'jpeg', quality=80) | |
data = buf.getvalue() | |
# Afficher la taille de l'image JPG en octets | |
print(len(data)) | |
else: | |
# Relancer le processus? | |
break |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment