1
0

Implement API key verification system

This commit is contained in:
erickahmed
2022-08-09 23:46:24 +02:00
committed by Erick
parent c80ff82c62
commit 6188192c7d
+28 -13
View File
@@ -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
@@ -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.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 = []
@@ -100,18 +117,16 @@ while True:
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()
# 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()