class Cam: def __init__(self): self.camera = Camera() def startRecording(self): self.stream = Stream(self.camera, file_name=self.file_name) def stopRecording(self): self.stream.end() self.camera.end() def convertToGif(self): clip = VideoFileClip(f"{self.file_name}_0.avi") clip.write_gif(f"{self.file_name}.gif") return f"{self.file_name}.gif"
dur = 10 # random default used_ids = [] while True: ans = input( "\nMain menu:\n\t'r' to record\n\t'd' to display\n\t'reset' to reset camera\n\t'q' to quit\n" ) if ans == 'q': break elif ans == 'd': # inspect camera d = Display(cam) elif ans == 'reset': cam.end() cam = Camera(resolution=resolution, fps=fps) elif ans == 'r': expt_subname = input('Enter trial ID: ').replace('/', '-') if expt_subname in used_ids: print('\nTrial ID already used.') continue expt_file = os.path.join(exp_data_dir, expt_name + '_' + expt_subname) expt_file += '.avi' dur = int_input( '\nEnter experiment duration in seconds:\nHit enter to default to previous (={} secs)\n' .format(dur), default=dur)
out2.write(img4) except: if (vidout == True): cv2.putText(img2, 'X,Y,Z = [? ? ?]', (0, h - 50), font, 0.8, (0, 255, 0), 2, cv2.LINE_AA) out.write(img2) cv2.putText(img4, 'X,Y,Z = [? ? ?]', (0, h - 50), font, 0.8, (0, 255, 0), 2, cv2.LINE_AA) out2.write(img4) i = i + 1 if (vidout == True): out.release() out2.release() totdist = np.asarray(totdistance) #c.release() c.end() sort = np.sort(totdist) min = sort[:10] sort = sort[::-1] max = sort[:10] minavg = min.mean() maxavg = max.mean() totavg = (minavg + maxavg) / 2 sdom = max.std() err.append(sdom) maxima.append((maxavg - totavg) / maxy) #omega = int(fname.split('_')[1]) #r.append(omega) #print("Finished " + str(omega)) # Plot the results # if(grphout ==True):
def main(cam, show, pseye): global processing if pseye == 1: cap = Camera([0], fps=60, resolution=Camera.RES_LARGE, colour=False) frame, timestamp = cap.read() # width, height = frame.shape else: cap = cv2.VideoCapture(cam) sz = 80 run = True vals = {} tiles = [] mz = cv2.imread("live_full_mz/live_full_mz.png") c = 1 last_diff = 0 # cv2.namedWindow("mz", cv2.WND_PROP_FULLSCREEN) # cv2.setWindowProperty("mz", cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN) if show == 0: window = False else: window = True while (run): try: if pseye == 1: frame, timestamp = cap.read() else: ret, frame = cap.read() small = cv2.resize(frame, (0, 0), fx=0.18, fy=0.18) width, height = small.shape ret, thr = cv2.threshold(small, 1, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU) kernel = np.ones((1, 1), np.uint8) opening = cv2.morphologyEx(thr, cv2.MORPH_OPEN, kernel, iterations=2) bg = cv2.dilate(opening, kernel, iterations=3) if not processing: processing = True tiles = processTiles(width, height, sz, bg) for tile in tiles: tile = np.array(tile) if window: # cv2.imshow('cam', bg) for i, tile in enumerate(tiles): cv2.imshow(('%s' % i), tile) k = cv2.waitKey(1) if k == ord('q'): run = False c += 1 except Exception as e: print(e) pass # program closing, stop camera stream and close windows if pseye == 1: cap.end() else: cap.release() cv2.destroyAllWindows()
def main(cam, show, pseye): if pseye == 1: cap = Camera([0], fps=60, resolution=Camera.RES_LARGE, colour=False) # frame, timestamp = cap.read() # width, height = frame.shape else: cap = cv2.VideoCapture(cam) # width = floor(cap.get(3)) // 2 # height = floor(cap.get(4)) // 2 # sz = 100 run = True # vals = {} mz = cv2.imread("live_full_mz/live_full_mz.png") # mz_sm = cv2.resize(mz, (0,0), fx=0.3, fy=0.3) global processing c = 1 # last_diff = 0 cv2.namedWindow("mz", cv2.WND_PROP_FULLSCREEN) cv2.setWindowProperty("mz", cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN) cv2.setWindowProperty("mz", cv2.WND_PROP_ASPECT_RATIO, cv2.WINDOW_FREERATIO) if show == 0: window = False else: window = True while (run): try: if pseye == 1: frame, timestamp = cap.read() # width, height = frame.shape else: ret, frame = cap.read() frame = frame[100:320, 100:540] small = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25) # gray = cv2.cvtColor(small,cv2.COLOR_BGR2GRAY) ret, thr = cv2.threshold(small, 1, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU) kernel = np.ones((1, 1), np.uint8) opening = cv2.morphologyEx(thr, cv2.MORPH_OPEN, kernel, iterations=1) bg = cv2.dilate(opening, kernel, iterations=2) # if c < 50: if not processing: # diff = detect_diffs_two(vals, c, width, height, sz, bg) # print(diff - last_diff) # d_sum = diff - last_diff # if d_sum > 0.005 or d_sum < -0.005: processing = True mz = processFrame(bg) try: mz = np.array(mz) # mz = cv2.resize(mz, (0, 0), fx=4, fy=4) except Exception as e: print(e) pass # mz = cv2.imread("live_full_mz/live_full_mz.png") # last_diff = diff # mz_sm = cv2.resize(mz, (0,0), fx=0.3, fy=0.3) # mz - cv2.imread("live_full/", 1) # detect_diffs(vals, c, width, height, sz, bg) # print("----") if window: try: # cv2.imshow('cam', bg) cv2.imshow('mz', mz) except Exception as e: print(e) pass k = cv2.waitKey(1) # if k == ord('+'): # mz = cv2.resize(mz, (0,0), fx=1.1, fy=1.1) # if k == ord('-'): # mz = cv2.resize(mz, (0,0), fx=0.9, fy=0.9) if k == ord('q'): run = False c += 1 except Exception as e: print(e) pass if pseye == 1: cap.end() else: cap.release() cv2.destroyAllWindows()