add mask test python code
This commit is contained in:
parent
2773887a7f
commit
c68de18588
2 changed files with 94 additions and 0 deletions
54
Mask_Efficacy/process_run.py
Executable file
54
Mask_Efficacy/process_run.py
Executable file
|
|
@ -0,0 +1,54 @@
|
||||||
|
import time
|
||||||
|
import imageio
|
||||||
|
from skimage.color import rgb2gray
|
||||||
|
import matplotlib.pyplot as plt
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
|
THRESH = 0.3
|
||||||
|
|
||||||
|
RUN = int(input('Enter run number: '))
|
||||||
|
|
||||||
|
vid = imageio.get_reader('run_{:03d}.mp4'.format(RUN), 'ffmpeg')
|
||||||
|
|
||||||
|
#----------------
|
||||||
|
# MAIN PROCESSING
|
||||||
|
#----------------
|
||||||
|
frame_data = []
|
||||||
|
start = time.monotonic()
|
||||||
|
# go through video frame by frame
|
||||||
|
print("Processing", end='')
|
||||||
|
for frame in vid:
|
||||||
|
print('.', end='', flush=True)
|
||||||
|
frame_bin = rgb2gray(frame) > THRESH
|
||||||
|
frame_count = np.count_nonzero(frame_bin == True)
|
||||||
|
frame_percent = 100 * frame_count / (1920*1080)
|
||||||
|
frame_data.append((frame_count, frame_percent))
|
||||||
|
# overall stats
|
||||||
|
avg_count = sum([x[0] for x in frame_data]) / len(frame_data)
|
||||||
|
avg_percent = 100 * avg_count / (1920*1080)
|
||||||
|
|
||||||
|
end = time.monotonic()
|
||||||
|
print("\nProcessing done in {} secs.".format(end - start))
|
||||||
|
print("Average Count = {}".format(avg_count))
|
||||||
|
print("Average Percent = {}".format(avg_percent))
|
||||||
|
|
||||||
|
#-------------
|
||||||
|
# SAVE TO FILE
|
||||||
|
#-------------
|
||||||
|
print("Saving data to file...")
|
||||||
|
with open('run_{:03d}.csv'.format(RUN), 'w') as fp:
|
||||||
|
for frame, data in enumerate(frame_data):
|
||||||
|
fp.write('{},{},{}\n'.format(frame, data[0], data[1]))
|
||||||
|
|
||||||
|
#---------
|
||||||
|
# PLOTTING
|
||||||
|
#---------
|
||||||
|
print("Generating plots...")
|
||||||
|
fig, ax = plt.subplots(1, figsize = (10,5))
|
||||||
|
ax.set_title("RUN {:03d}\nTHRESH = {}, AVG_CNT = {:4.2}, AVG_PER = {:.3}".format(RUN, THRESH,avg_count, avg_percent))
|
||||||
|
ax.set_xlabel("FRAME")
|
||||||
|
ax.set_ylabel("COUNT")
|
||||||
|
ax.plot([x[0] for x in frame_data])
|
||||||
|
fig.savefig('run_{:03d}_plot.png'.format(RUN))
|
||||||
|
|
||||||
|
print("DONE.")
|
||||||
40
Mask_Efficacy/take_video.py
Executable file
40
Mask_Efficacy/take_video.py
Executable file
|
|
@ -0,0 +1,40 @@
|
||||||
|
import time
|
||||||
|
import math
|
||||||
|
import os
|
||||||
|
import RPi.GPIO as GPIO
|
||||||
|
import simpleaudio as sa
|
||||||
|
import picamera
|
||||||
|
|
||||||
|
camera = picamera.PiCamera()
|
||||||
|
camera.resolution = (1920, 1080)
|
||||||
|
VIDEO_LENGTH = 10
|
||||||
|
|
||||||
|
BUTTON = 4
|
||||||
|
GPIO.setmode(GPIO.BCM)
|
||||||
|
GPIO.setup(BUTTON, GPIO.IN, pull_up_down=GPIO.PUD_UP)
|
||||||
|
|
||||||
|
SIN_LENGTH = 500
|
||||||
|
SIN_AMPLITUDE = 127
|
||||||
|
SIN_OFFSET = 128
|
||||||
|
DELTA_PI = 2 * math.pi / SIN_LENGTH
|
||||||
|
sine_wave = bytes([
|
||||||
|
int(SIN_OFFSET + SIN_AMPLITUDE * math.sin(DELTA_PI * i)) for i in range(SIN_LENGTH)
|
||||||
|
])
|
||||||
|
|
||||||
|
def play_tone(length):
|
||||||
|
play_back = sa.play_buffer(sine_wave*length, 2, 2, 44100)
|
||||||
|
play_back.wait_done()
|
||||||
|
|
||||||
|
run_number = int(input("Enter run number:"))
|
||||||
|
|
||||||
|
print("Press button when ready.")
|
||||||
|
while GPIO.input(BUTTON):
|
||||||
|
pass
|
||||||
|
|
||||||
|
play_tone(100)
|
||||||
|
camera.start_recording("run_{:03d}.h264".format(run_number))
|
||||||
|
camera.wait_recording(VIDEO_LENGTH)
|
||||||
|
camera.stop_recording()
|
||||||
|
play_tone(100)
|
||||||
|
|
||||||
|
err = os.system("MP4Box -add run_{0:03d}.h264 run_{0:03d}.mp4".format(run_number))
|
||||||
Loading…
Reference in a new issue