def main(): thicc = 2 # TODO: set sleep threshold to 8 score = 0 # To evaluate the state of the driver(drowsy or not) path = os.getcwd() font = cv2.FONT_HERSHEY_COMPLEX_SMALL cap = cv2.VideoCapture(0) while True: ret, frame = cap.read() height, width = frame.shape[:2] classifier = Classifier(frame) left_eye_pred = classifier.left_eye() right_eye_pred = classifier.right_eye() if left_eye_pred == 0 and right_eye_pred == 0: score += 1 cv2.putText(frame, "Asleep", (10, height - 20), font, 1, (255, 255, 255), 1, cv2.LINE_AA) else: score = -1 cv2.putText(frame, "Awake", (10, height - 20), font, 1, (255, 255, 255), 1, cv2.LINE_AA) if score < 0: score = 0 cv2.putText(frame, "Score: " + str(score), (100, height - 20), font, 1, (255, 255, 255), 1, cv2.LINE_AA) if score > 15: # Using 15 as threshold to say the driver has had his/her eyes closed for too long # Driver is feeling sleepy so we play the alarm cv2.imwrite(os.path.join(path, str(datetime.now) + '.jpg'), frame) # TODO: Fix code crash after sound play playsound() # Play sound if thicc < 16: thicc += 2 else: thicc -= 2 if thicc < 2: thicc = 2 cv2.readOpticalFlow(frame, (0, 0), (width, height), (0, 0, 255), thicc) cv2.imshow('frame', frame) if cv2.waitKey(1) & 0xFF == ord('q'): break cap.release() cv2.destroyAllWindows()
def test_with_image(img_path): thicc = 2 score = 0 # To evaluate the state of the driver(drowsy or not) path = os.getcwd() font = cv2.FONT_HERSHEY_COMPLEX_SMALL img = cv2.imread(img_path) height, width = img.shape[:2] classifier = Classifier(img) left_eye_pred = classifier.left_eye() right_eye_pred = classifier.right_eye() if left_eye_pred == 0 and right_eye_pred == 0: score += 1 cv2.putText(img, "Closed", (10, height - 20), font, 1, (255, 255, 255), 1, cv2.LINE_AA) else: score = -1 cv2.putText(img, "Opened", (10, height - 20), font, 1, (255, 255, 255), 1, cv2.LINE_AA) if score < 0: score = 0 cv2.putText(img, "Score: " + str(score), (100, height - 20), font, 1, (255, 255, 255), 1, cv2.LINE_AA) if score > 15: # Using 15 as threshold to say the driver has had his/her eyes closed for too long # Driver is feeling sleepy so we play the alarm cv2.imwrite(os.path.join(path, str(datetime.now) + '.jpg'), img) #playsound() # Play sound if thicc < 16: thicc += 2 else: thicc -= 2 if thicc < 2: thicc = 2 cv2.readOpticalFlow(img, (0, 0), (width, height), (0, 0, 255), thicc) cv2.imshow('frame', img) cv2.waitKey(0) cv2.destroyAllWindows()
def test_edgeaware_interpolator(self): # readGT MAX_DIF = 1.0 MAX_MEAN_DIF = 1.0 / 256.0 src = cv.imread(self.find_file("cv/optflow/RubberWhale1.png"), cv.IMREAD_COLOR) self.assertFalse(src is None) ref_flow = cv.readOpticalFlow( self.find_file( "cv/sparse_match_interpolator/RubberWhale_reference_result.flo" )) self.assertFalse(ref_flow is None) matches = np.genfromtxt( self.find_file( "cv/sparse_match_interpolator/RubberWhale_sparse_matches.txt") ).astype(np.float32) from_points = matches[:, 0:2] to_points = matches[:, 2:4] interpolator = cv.ximgproc.createEdgeAwareInterpolator() interpolator.setK(128) interpolator.setSigma(0.05) interpolator.setUsePostProcessing(True) interpolator.setFGSLambda(500.0) interpolator.setFGSSigma(1.5) dense_flow = interpolator.interpolate(src, from_points, src, to_points) self.assertTrue(cv.norm(dense_flow, ref_flow, cv.NORM_INF) <= MAX_DIF) self.assertTrue( cv.norm(dense_flow, ref_flow, cv.NORM_L1) <= ( MAX_MEAN_DIF * dense_flow.shape[0] * dense_flow.shape[1]))
def get_img_pair_and_opticalflow(self, idx): sharp_file_path = self.sharp_file_paths[idx] blur_file_path = sharp_file_path.replace("sharp", "blur") opticalflow_file_path = sharp_file_path.replace("sharp", "opticalflow") opticalflow_file_path = opticalflow_file_path.replace("png", "flo") img_input = Image.open(blur_file_path).convert('RGB') img_target = Image.open(sharp_file_path).convert('RGB') opticalflow_1 = cv2.readOpticalFlow(opticalflow_file_path) opticalflow_folder = os.path.dirname(opticalflow_file_path) flow1_name = os.path.basename(opticalflow_file_path) files = os.listdir(opticalflow_folder) index = files.index(flow1_name) opticalflow_2 = cv2.readOpticalFlow(os.path.join(opticalflow_folder, files[index+1])) return img_input, img_target, opticalflow_1, opticalflow_2
def test_ric_interpolator(self): # readGT MAX_DIF = 6.0 MAX_MEAN_DIF = 60.0 / 256.0 src0 = cv.imread(self.find_file("cv/optflow/RubberWhale1.png"), cv.IMREAD_COLOR) self.assertFalse(src0 is None) src1 = cv.imread(self.find_file("cv/optflow/RubberWhale2.png"), cv.IMREAD_COLOR) self.assertFalse(src1 is None) ref_flow = cv.readOpticalFlow( self.find_file( "cv/sparse_match_interpolator/RubberWhale_reference_result.flo" )) self.assertFalse(ref_flow is None) matches = np.genfromtxt( self.find_file( "cv/sparse_match_interpolator/RubberWhale_sparse_matches.txt") ).astype(np.float32) from_points = matches[:, 0:2] to_points = matches[:, 2:4] interpolator = cv.ximgproc.createRICInterpolator() interpolator.setK(32) interpolator.setSuperpixelSize(15) interpolator.setSuperpixelNNCnt(150) interpolator.setSuperpixelRuler(15.0) interpolator.setSuperpixelMode(cv.ximgproc.SLIC) interpolator.setAlpha(0.7) interpolator.setModelIter(4) interpolator.setRefineModels(True) interpolator.setMaxFlow(250) interpolator.setUseVariationalRefinement(True) interpolator.setUseGlobalSmootherFilter(True) interpolator.setFGSLambda(500.0) interpolator.setFGSSigma(1.5) dense_flow = interpolator.interpolate(src0, from_points, src1, to_points) self.assertTrue(cv.norm(dense_flow, ref_flow, cv.NORM_INF) <= MAX_DIF) self.assertTrue( cv.norm(dense_flow, ref_flow, cv.NORM_L1) <= ( MAX_MEAN_DIF * dense_flow.shape[0] * dense_flow.shape[1]))
def evaluate_of(of_alg, name): print("\nEvaluating: " + name + " algorithm:") sum_mepe = 0 for dataset in datasets: first = cv.imread('./data/' + dataset + "/frame10.png", cv.IMREAD_GRAYSCALE) second = cv.imread('./data/' + dataset + "/frame11.png", cv.IMREAD_GRAYSCALE) flow_gt = cv.readOpticalFlow('./flow/' + dataset + '/flow10.flo') #flow_gt_img = fl.flow_to_image(flow_gt) #plt.imshow(flow_gt_img) #plt.show() flow = of_alg.calc(first, second, None) #flow_img = fl.flow_to_image(flow_gt) #plt.imshow(flow_img) #plt.show() mepe = fl.evaluate_flow(flow_gt, flow) print("\tResult for " + dataset + " dataset: EPE= %f" % (mepe)) sum_mepe += mepe print("\tAverage EPE of " + name + " algorithm is %f" % (sum_mepe / 8))
def test_with_image(img_path): VGG_Face = Vgg_face_dag() VGG_Face = vgg_face_dag(VGG_Face, "src/models/vgg_face_dag.pth") thicc = 2 score = 0 # To evaluate the state of the driver(drowsy or not) frame_count = 0 frames = [] path = os.getcwd() font = cv2.FONT_HERSHEY_COMPLEX_SMALL img = cv2.imread(img_path) height, width = img.shape[:2] classifier = Classifier(img) left_eye_pred = classifier.left_eye() right_eye_pred = classifier.right_eye() frames.append(img) drunk_pred = classifier.drunk_pred(frames, VGG_Face) if drunk_pred == 1: cv2.putText(img, "Drunk", (10, 20), font, 1, (255, 255, 255), 1, cv2.LINE_AA) else: cv2.putText(img, "Sober", (10, 20), font, 1, (255, 255, 255), 1, cv2.LINE_AA) if left_eye_pred == 0 and right_eye_pred == 0: score += 1 cv2.putText(img, "Asleep", (10, height - 20), font, 1, (255, 255, 255), 1, cv2.LINE_AA) else: score = -1 cv2.putText(img, "Awake", (10, height - 20), font, 1, (255, 255, 255), 1, cv2.LINE_AA) if score < 0: score = 0 cv2.putText(img, "Score: " + str(score), (100, height - 20), font, 1, (255, 255, 255), 1, cv2.LINE_AA) if score > 8: # Using 15 as threshold to say the driver has had his/her eyes closed for too long # Driver is feeling sleepy so we play the alarm cv2.imwrite(os.path.join(path, str(datetime.now) + '.jpg'), img) #playsound() # Play sound if thicc < 16: thicc += 2 else: thicc -= 2 if thicc < 2: thicc = 2 cv2.readOpticalFlow(img, (0, 0), (width, height), (0, 0, 255), thicc) cv2.imshow('frame', img) k = cv2.waitKey(0) if k == 27: cv2.destroyAllWindows()
def preprocess_dataset(data_dir): sub_folders = os.listdir(data_dir) sharp_file_paths = [] device = torch.device("cuda" if torch.cuda.is_available() else "cpu") df = pd.DataFrame(columns=[ 'sharp_path', 'blur_path', 'opticalflow_1_path', 'opticalflow_2_path' ]) folder_number = 0 for folder_name in sub_folders: if (folder_name == 'blur_img_all.pt' or folder_name == 'sharp_img_all.pt' or folder_name == 'opticalflow_all.pt' or folder_name == 'log.xlsx' or folder_name == 'log.csv' or folder_name == '.ipynb_checkpoints'): continue folder_number += 1 sharp_sub_folder = os.path.join(data_dir, folder_name, 'sharp') sharp_file_names = os.listdir(sharp_sub_folder) for file_name in sharp_file_names: if (sharp_file_names.index(file_name) != 0 and sharp_file_names.index(file_name) != len(sharp_file_names) - 1): sharp_file_path = os.path.join(sharp_sub_folder, file_name) sharp_file_paths.append(sharp_file_path) n_samples = len(sharp_file_paths) blur_img_all = torch.zeros((n_samples, 3, 256, 256), dtype=torch.float16) sharp_img_all = torch.zeros((n_samples, 3, 256, 256), dtype=torch.float16) opticalflow_all = torch.zeros((n_samples, 2, 2, 128, 128), dtype=torch.float16) for idx in range(n_samples): sharp_path = sharp_file_paths[idx] blur_path = sharp_path.replace("sharp", "blur") opticalflow_1_path = sharp_path.replace("sharp", "opticalflow") opticalflow_1_path = opticalflow_1_path.replace("png", "flo") opticalflow_folder = os.path.dirname(opticalflow_1_path) flow1_name = os.path.basename(opticalflow_1_path) files = os.listdir(opticalflow_folder) index = files.index(flow1_name) opticalflow_2_path = os.path.join(opticalflow_folder, files[index + 1]) """ print(os.path.basename(blur_path)) print(os.path.basename(sharp_path)) print(flow1_name) print(files[index+1]) """ print("prepocessing: ", idx) new_row = { 'sharp_path': os.path.basename(sharp_path), 'blur_path': os.path.basename(blur_path), 'opticalflow_1_path': flow1_name, 'opticalflow_2_path': files[index + 1] } df = df.append(new_row, ignore_index=True) img_input = Image.open(blur_path).convert('RGB') img_target = Image.open(sharp_path).convert('RGB') opticalflow_1 = cv2.readOpticalFlow(opticalflow_1_path) opticalflow_2 = cv2.readOpticalFlow(opticalflow_2_path) img_input = transforms.ToTensor()(img_input).to(device) img_target = transforms.ToTensor()(img_target).to(device) opticalflow_1 = torch.Tensor(opticalflow_1).to(device) opticalflow_2 = torch.Tensor(opticalflow_2).to(device) img_input, img_target, opticalflow_1, opticalflow_2 = transform( img_input, img_target, opticalflow_1, opticalflow_2, 256) blur_img_all[idx, :, :, :] = img_input sharp_img_all[idx, :, :, :] = img_target opticalflow_all[idx, 0, :, :, :] = opticalflow_1 opticalflow_all[idx, 1, :, :, :] = opticalflow_2 torch.save(blur_img_all, os.path.join(data_dir, 'blur_img_all.pt')) torch.save(sharp_img_all, os.path.join(data_dir, 'sharp_img_all.pt')) torch.save(opticalflow_all, os.path.join(data_dir, 'opticalflow_all.pt')) #blur_img_all, tensor, range[0,1], dtype = torch.float16, size = (2059, 3, 256, 256) #sharp_img_all, tensor, range[0,1], dtype = torch.float16, size = (2059, 3, 256, 256) #opticalflow_all, tensor, range[0,255], dtype = torch.float16, size = (2059, 2, 2, 128, 128) # save prepocess file path df.to_excel(os.path.join(data_dir, 'log.xlsx')) df.to_csv((os.path.join(data_dir, 'log.csv')), index=False)
import cv2 from warping.warp import visulize_flow_file, optical_flow_warping import torch import os if __name__ == '__main__': base_dir = os.path.dirname(__file__) # visualize the optical flow here visulize_flow_file(os.path.join(base_dir, 'reference_frame_0010.flo')) test = cv2.readOpticalFlow( os.path.join(base_dir, 'reference_frame_0010.flo')) test = torch.from_numpy(test) H, W, C = test.shape test = test.permute(2, 0, 1).unsqueeze(0) test_img2 = cv2.imread(os.path.join(base_dir, 'frame_0011.png'))[:, :, ::-1] test_img2 = test_img2 / 255.0 test_img2 = torch.from_numpy(test_img2).float() test_img2 = test_img2.permute(2, 0, 1).unsqueeze(0) out = optical_flow_warping(test_img2, test)[0] out = out.permute(1, 2, 0).numpy() # plt.imshow(out) # plt.show() cv2.imwrite(os.path.join(base_dir, 'warped.png'), out[:, :, ::-1] * 255)
def main(): VGG_Face = Vgg_face_dag() VGG_Face = vgg_face_dag(VGG_Face, "src/models/vgg_face_dag.pth") thicc = 2 score = 0 # To evaluate the state of the driver(drowsy or not) frame_count = 0 frames = [] path = os.getcwd() font = cv2.FONT_HERSHEY_COMPLEX_SMALL cap = cv2.VideoCapture(0) while True: ret, frame = cap.read() height, width = frame.shape[:2] classifier = Classifier(frame) left_eye_pred = classifier.left_eye() right_eye_pred = classifier.right_eye() frames.append(frame) if len(frames) == 10: drunk_pred = classifier.drunk_pred(frames, VGG_Face) frames = [] if drunk_pred == 1: cv2.putText(frame, "Drunk", (width-10, height-20), font, 1, (255, 255, 255), 1, cv2.LINE_AA) else: cv2.putText(frame, "Sober", (width-10, height-20), font, 1, (255, 255, 255), 1, cv2.LINE_AA) if left_eye_pred == 0 and right_eye_pred == 0: score += 1 cv2.putText(frame, "Asleep", (10, height-20), font, 1, (255, 255, 255), 1, cv2.LINE_AA) else: score = -1 cv2.putText(frame, "Awake", (10, height-20), font, 1, (255, 255, 255), 1, cv2.LINE_AA) if score < 0: score = 0 cv2.putText(frame, "Score: "+str(score), (100, height-20), font, 1, (255, 255, 255), 1, cv2.LINE_AA) if score >= 8: # Using 8 as threshold to say the driver has had his/her eyes closed for too long # Driver is feeling sleepy so we play the alarm #cv2.imwrite(os.path.join(path, str(datetime.now)+'.jpg'), frame) # TODO: Fix code crash after sound play playsound() # Play sound if thicc < 16: thicc += 2 else: thicc -= 2 if thicc < 2: thicc = 2 cv2.readOpticalFlow(frame, (0, 0), (width, height), (0, 0, 255), thicc) cv2.imshow('frame', frame) k = cv2.waitKey(1) if k == 27: cv2.destroyAllWindows()
parser.add_argument('-f', '--flow_name', required=True) args = parser.parse_args() img = cv2.imread('%s/%s' % (args.image_dir, args.image_name)) # print('img:',img.shape) # img = img[:,:128,:] # print('img:',img.shape) # img = cv2.resize(img, (int(img.shape[0]*2.0), int(img.shape[1]*2.0))) # print('img:',img.shape) # cv2.imwrite('warped_image/moving-gif/00130/0.png', img) img = torch.from_numpy(img.astype(np.float32)) img = img.unsqueeze(0).permute(0, 3, 1, 2) # print('img:',img.size()) img = Variable(img).cuda() flow = cv2.readOpticalFlow('%s/%s' % (args.flow_dir, args.flow_name)) flow = torch.from_numpy(flow.astype(np.float32)) flow = flow.unsqueeze(0).permute(0, 3, 1, 2) # print('flow:',flow.size()) flow = Variable(flow).cuda() warped_img = warp(img, flow) warped_img = warped_img.squeeze(0).permute(1, 2, 0) # print('warped_img:',warped_img.size()) warped_img = warped_img.cpu() warped_img = np.float32(warped_img) cv2.imwrite('Input/Paint/moving-gif_00058_%s.png' % (args.flow_name[:-4]), warped_img) # cv2.imwrite('Input/Editing/3-VAM9owl-o/start_scale=2,input=0.5/%s.png' % ( args.flow_name[:-4]), warped_img) # if __name__ == '__main__':