forked from mengxin513/data_important_26102018
-
Notifications
You must be signed in to change notification settings - Fork 0
/
drift.py
98 lines (83 loc) · 3.38 KB
/
drift.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
from __future__ import print_function
import io
import sys
import os
import time
import argparse
import numpy as np
import picamera
from builtins import input
from readchar import readchar, readkey
from openflexure_stage import OpenFlexureStage
from openflexure_microscope import load_microscope
from openflexure_microscope.microscope import picamera_supports_lens_shading
import scipy
from scipy import ndimage
import matplotlib.pyplot as plt
from contextlib import contextmanager, closing
import data_file
import cv2
from camera_stuff import find_template
#import h5py
import threading
import queue
def image_capture(start_t, event, ms, q):
while event.is_set():
frame = ms.rgb_image().astype(np.float32)
capture_t = time.time()
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
q.put(frame)
tim = capture_t - start_t
q.put(tim)
print('Number of itms in the queue: {}'.format(q.qsize()))
time.sleep(0.2)
if __name__ == "__main__":
with load_microscope("microscope_settings.npz") as ms, \
closing(data_file.Datafile(filename = "drift.hdf5")) as df:
assert picamera_supports_lens_shading(), "You need the updated picamera module with lens shading!"
camera = ms.camera
stage = ms.stage
camera.resolution=(640,480)
cam_pos = df.new_group("data", "drift")
N_frames = 500
#need to be consistant between drift.py and drift_plot.py
camera.start_preview(resolution=(640,480))
image = ms.rgb_image().astype(np.float32)
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
mean = np.mean(image)
templ8 = (image - mean)[100:-100, 100:-100]
cam_pos['template'] = templ8
cam_pos['initial_image'] = image
imgfile_location = "/home/pi/summer/drift/calibration/drift_templ8.jpg"
cv2.imwrite(imgfile_location, templ8)
imgfile_location = "/home/pi/summer/drift/calibration/drift_image.jpg"
cv2.imwrite(imgfile_location, image)
q = queue.Queue()
event = threading.Event()
start_t = time.time()
t = threading.Thread(target = image_capture, args = (start_t, event, ms, q), name = 'thread1')
event.set()
t.start()
try:
while event.is_set():
if not q.empty():
data = np.zeros((N_frames, 3))
for i in range(N_frames):
frame = q.get()
tim = q.get()
data[i, 0] = tim
data[i, 1:], corr = find_template(templ8, frame - np.mean(frame), return_corr = True, fraction=0.5)
df.add_data(data, cam_pos, "data")
imgfile_location_1 = "/home/pi/summer/drift/frames/drift_%s.jpg" % time.strftime("%02Y.%02m.%02d_%02H:%02M:%02S")
imgfile_location_2 = "/home/pi/summer/drift/frames/corr_%s.jpg" % time.strftime("%02Y.%02m.%02d_%02H:%02M:%02S")
cv2.imwrite(imgfile_location_1, frame)
cv2.imwrite(imgfile_location_2, corr * 255.0 / np.max(corr))
else:
time.sleep(0.5)
print("Looping")
print("Done")
except KeyboardInterrupt:
event.clear()
t.join()
camera.stop_preview()
print ("Got a keyboard interrupt, stopping")