Skip to content

Commit 0c937d1

Browse files
authored
Merge pull request adafruit#1264 from caternuson/mask_code
Add mask test python code
2 parents 2773887 + c68de18 commit 0c937d1

File tree

2 files changed

+94
-0
lines changed

2 files changed

+94
-0
lines changed

Mask_Efficacy/process_run.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import time
2+
import imageio
3+
from skimage.color import rgb2gray
4+
import matplotlib.pyplot as plt
5+
import numpy as np
6+
7+
THRESH = 0.3
8+
9+
RUN = int(input('Enter run number: '))
10+
11+
vid = imageio.get_reader('run_{:03d}.mp4'.format(RUN), 'ffmpeg')
12+
13+
#----------------
14+
# MAIN PROCESSING
15+
#----------------
16+
frame_data = []
17+
start = time.monotonic()
18+
# go through video frame by frame
19+
print("Processing", end='')
20+
for frame in vid:
21+
print('.', end='', flush=True)
22+
frame_bin = rgb2gray(frame) > THRESH
23+
frame_count = np.count_nonzero(frame_bin == True)
24+
frame_percent = 100 * frame_count / (1920*1080)
25+
frame_data.append((frame_count, frame_percent))
26+
# overall stats
27+
avg_count = sum([x[0] for x in frame_data]) / len(frame_data)
28+
avg_percent = 100 * avg_count / (1920*1080)
29+
30+
end = time.monotonic()
31+
print("\nProcessing done in {} secs.".format(end - start))
32+
print("Average Count = {}".format(avg_count))
33+
print("Average Percent = {}".format(avg_percent))
34+
35+
#-------------
36+
# SAVE TO FILE
37+
#-------------
38+
print("Saving data to file...")
39+
with open('run_{:03d}.csv'.format(RUN), 'w') as fp:
40+
for frame, data in enumerate(frame_data):
41+
fp.write('{},{},{}\n'.format(frame, data[0], data[1]))
42+
43+
#---------
44+
# PLOTTING
45+
#---------
46+
print("Generating plots...")
47+
fig, ax = plt.subplots(1, figsize = (10,5))
48+
ax.set_title("RUN {:03d}\nTHRESH = {}, AVG_CNT = {:4.2}, AVG_PER = {:.3}".format(RUN, THRESH,avg_count, avg_percent))
49+
ax.set_xlabel("FRAME")
50+
ax.set_ylabel("COUNT")
51+
ax.plot([x[0] for x in frame_data])
52+
fig.savefig('run_{:03d}_plot.png'.format(RUN))
53+
54+
print("DONE.")

Mask_Efficacy/take_video.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import time
2+
import math
3+
import os
4+
import RPi.GPIO as GPIO
5+
import simpleaudio as sa
6+
import picamera
7+
8+
camera = picamera.PiCamera()
9+
camera.resolution = (1920, 1080)
10+
VIDEO_LENGTH = 10
11+
12+
BUTTON = 4
13+
GPIO.setmode(GPIO.BCM)
14+
GPIO.setup(BUTTON, GPIO.IN, pull_up_down=GPIO.PUD_UP)
15+
16+
SIN_LENGTH = 500
17+
SIN_AMPLITUDE = 127
18+
SIN_OFFSET = 128
19+
DELTA_PI = 2 * math.pi / SIN_LENGTH
20+
sine_wave = bytes([
21+
int(SIN_OFFSET + SIN_AMPLITUDE * math.sin(DELTA_PI * i)) for i in range(SIN_LENGTH)
22+
])
23+
24+
def play_tone(length):
25+
play_back = sa.play_buffer(sine_wave*length, 2, 2, 44100)
26+
play_back.wait_done()
27+
28+
run_number = int(input("Enter run number:"))
29+
30+
print("Press button when ready.")
31+
while GPIO.input(BUTTON):
32+
pass
33+
34+
play_tone(100)
35+
camera.start_recording("run_{:03d}.h264".format(run_number))
36+
camera.wait_recording(VIDEO_LENGTH)
37+
camera.stop_recording()
38+
play_tone(100)
39+
40+
err = os.system("MP4Box -add run_{0:03d}.h264 run_{0:03d}.mp4".format(run_number))

0 commit comments

Comments
 (0)