Implement API key verification system
This commit is contained in:
@@ -1,11 +1,11 @@
|
|||||||
|
from genericpath import isfile
|
||||||
import io
|
import io
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
import numpy as np
|
import numpy as np
|
||||||
import cv2
|
import cv2
|
||||||
|
import json
|
||||||
from pyautogui import size
|
from pyautogui import size
|
||||||
|
|
||||||
# Imports the Google Cloud client library
|
|
||||||
from google.cloud import vision
|
from google.cloud import vision
|
||||||
|
|
||||||
# Minimum score required for labels detection
|
# Minimum score required for labels detection
|
||||||
@@ -30,9 +30,26 @@ def shape_selection(event, x, y, flags, param):
|
|||||||
cv2.rectangle(image, ref_point[0], ref_point[1], (0, 255, 0), 2)
|
cv2.rectangle(image, ref_point[0], ref_point[1], (0, 255, 0), 2)
|
||||||
cv2.imshow("image", image)
|
cv2.imshow("image", image)
|
||||||
|
|
||||||
key_rel_path= os.path.join('..', 'keys', 'developerKey.json')
|
# If a key is present in /keys and has a correct name, add it to enviroment variable
|
||||||
key = os.path.abspath(key_rel_path)
|
key_folder = os.path.join("..", "keys")
|
||||||
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = key
|
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
|
# User input from the command line
|
||||||
image_name = sys.argv[1]
|
image_name = sys.argv[1]
|
||||||
@@ -47,11 +64,11 @@ if not(os.path.exists(file_name)):
|
|||||||
|
|
||||||
# Press q to exit or enter correct path
|
# Press q to exit or enter correct path
|
||||||
userin = input("Enter the correct path or press q to quit: ")
|
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
|
file_name = userin
|
||||||
break
|
break
|
||||||
else:
|
|
||||||
sys.exit(0)
|
|
||||||
|
|
||||||
# Initialize the list of reference point
|
# Initialize the list of reference point
|
||||||
ref_point = []
|
ref_point = []
|
||||||
@@ -100,18 +117,16 @@ while True:
|
|||||||
break
|
break
|
||||||
|
|
||||||
if len(ref_point) == 2:
|
if len(ref_point) == 2:
|
||||||
crop_img = clone[ref_point[0][1]:ref_point[1][1], ref_point[0][0]:
|
crop_img = clone[ref_point[0][1]:ref_point[1][1], ref_point[0][0]:ref_point[1][0]]
|
||||||
ref_point[1][0]]
|
|
||||||
#cv2.imshow("crop_img", crop_img) #uncomment --> show cropped image
|
#cv2.imshow("crop_img", crop_img) #uncomment --> show cropped image
|
||||||
cv2.waitKey(0) #maybe useless
|
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
|
# close all open windows
|
||||||
cv2.destroyAllWindows()
|
cv2.destroyAllWindows()
|
||||||
|
|
||||||
# File used for label detection
|
# 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
|
# Loads the image into memory
|
||||||
with io.open(target_file, 'rb') as image_file:
|
with io.open(target_file, 'rb') as image_file:
|
||||||
content = image_file.read()
|
content = image_file.read()
|
||||||
|
|||||||
Reference in New Issue
Block a user