From 6188192c7d2e5fbcc7a3da28e1caf7430219bb5e Mon Sep 17 00:00:00 2001 From: erickahmed Date: Tue, 9 Aug 2022 23:46:24 +0200 Subject: [PATCH] Implement API key verification system --- vision-env/src/vision-test.py | 59 ++++++++++++++++++++++------------- 1 file changed, 37 insertions(+), 22 deletions(-) diff --git a/vision-env/src/vision-test.py b/vision-env/src/vision-test.py index 3728e65..da23e9b 100644 --- a/vision-env/src/vision-test.py +++ b/vision-env/src/vision-test.py @@ -1,11 +1,11 @@ +from genericpath import isfile import io import os import sys import numpy as np import cv2 +import json from pyautogui import size - -# Imports the Google Cloud client library from google.cloud import vision # Minimum score required for labels detection @@ -14,25 +14,42 @@ MIN_SCORE_REQUIRED = 0.80 def shape_selection(event, x, y, flags, param): # grab references to the global variables global ref_point, crop - + # if the left mouse button was clicked, record the starting # (x, y) coordinates and indicate that cropping is being performed if event == cv2.EVENT_LBUTTONDOWN: ref_point = [(x, y)] - + # check to see if the left mouse button was released elif event == cv2.EVENT_LBUTTONUP: # record the ending (x, y) coordinates and indicate that # the cropping operation is finished ref_point.append((x, y)) - + # draw a rectangle around the region of interest cv2.rectangle(image, ref_point[0], ref_point[1], (0, 255, 0), 2) cv2.imshow("image", image) -key_rel_path= os.path.join('..', 'keys', 'developerKey.json') -key = os.path.abspath(key_rel_path) -os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = key +# If a key is present in /keys and has a correct name, add it to enviroment variable +key_folder = os.path.join("..", "keys") +key_names = ["devKey.json", + "devkey.json", + "developerkey.json", + "developerKey.json"] + +for i in key_names: + success = False + key_rel_path = os.path.join(key_folder, i) + key_abs_path = os.path.abspath(key_rel_path) + + if os.path.exists(key_abs_path): + success = True + os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = key_abs_path + break + +if not success: + print("API keys not found on '~/$YOUR_ENV_NAME/keys'. Closing executable.") + sys.exit(0) # User input from the command line image_name = sys.argv[1] @@ -47,11 +64,11 @@ if not(os.path.exists(file_name)): # Press q to exit or enter correct path userin = input("Enter the correct path or press q to quit: ") - if userin != "q" or "Q": + if userin == "q" or "Q": + sys.exit(0) + else: file_name = userin break - else: - sys.exit(0) # Initialize the list of reference point ref_point = [] @@ -80,13 +97,13 @@ clone = image.copy() cv2.namedWindow("image") cv2.setMouseCallback("image", shape_selection) - + # keep looping until the 'q' key is pressed while True: # display the image and wait for a keypress cv2.imshow("image", image) key = cv2.waitKey(1) & 0xFF - + # press 'r' to reset the window if key == ord("r"): image = clone.copy() @@ -94,24 +111,22 @@ while True: # press 'q' to quit if key == ord("q"): sys.exit(0) - + # if the 'c' key is pressed, break from the loop elif key == ord("c"): break - + if len(ref_point) == 2: - crop_img = clone[ref_point[0][1]:ref_point[1][1], ref_point[0][0]: - ref_point[1][0]] + crop_img = clone[ref_point[0][1]:ref_point[1][1], ref_point[0][0]:ref_point[1][0]] #cv2.imshow("crop_img", crop_img) #uncomment --> show cropped image cv2.waitKey(0) #maybe useless - cv2.imwrite('copy1.jpg', crop_img) - + cv2.imwrite(os.path.join("..", "images/testset/test0/copy1.jpg"), crop_img) + # close all open windows -cv2.destroyAllWindows() +cv2.destroyAllWindows() # File used for label detection -target_file = os.path.abspath('copy1.jpg') - +target_file = os.path.abspath(os.path.join("..", "images/testset/test0/copy1.jpg")) # Loads the image into memory with io.open(target_file, 'rb') as image_file: content = image_file.read()