From 78bfdda45c191ed0b79c2ecb3a3ce3301f7cec59 Mon Sep 17 00:00:00 2001 From: Davide Tonelli Date: Sat, 23 Jul 2022 14:05:26 +0200 Subject: [PATCH 1/3] Script takes image path as input --- vision-env/src/vision-test.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/vision-env/src/vision-test.py b/vision-env/src/vision-test.py index b9dbd9f..794f8da 100644 --- a/vision-env/src/vision-test.py +++ b/vision-env/src/vision-test.py @@ -1,19 +1,23 @@ import io import os +import sys # Imports the Google Cloud client library from google.cloud import vision -# Instantiates a client -client = vision.ImageAnnotatorClient() +# User input from the command line +image_path = sys.argv[1] # The name of the image file to annotate -file_name = os.path.abspath('images/mmr.jpg') +file_name = os.path.abspath(image_path) # Loads the image into memory with io.open(file_name, 'rb') as image_file: content = image_file.read() +# Instantiates a client +client = vision.ImageAnnotatorClient() + image = vision.Image(content=content) # Performs label detection on the image file From 8cf977887200043e00bf8a0f687ae2a30bc3c934 Mon Sep 17 00:00:00 2001 From: Davide Tonelli Date: Sat, 23 Jul 2022 15:22:32 +0200 Subject: [PATCH 2/3] Add target rectangular selection --- vision-env/src/vision-test.py | 63 +++++++++++++++++++++++++++++++++-- 1 file changed, 60 insertions(+), 3 deletions(-) diff --git a/vision-env/src/vision-test.py b/vision-env/src/vision-test.py index 794f8da..f4ce5eb 100644 --- a/vision-env/src/vision-test.py +++ b/vision-env/src/vision-test.py @@ -1,23 +1,80 @@ import io import os import sys +import cv2 # Imports the Google Cloud client library from google.cloud import vision +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) + # User input from the command line -image_path = sys.argv[1] +image_name = sys.argv[1] # The name of the image file to annotate -file_name = os.path.abspath(image_path) +file_name = os.path.abspath(image_name) + +# Initialize the list of reference point +ref_point = [] +crop = False + +image = cv2.imread(file_name) +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() + + # 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]] + #cv2.imshow("crop_img", crop_img) #uncomment --> show cropped image + cv2.waitKey(0) #maybe useless + cv2.imwrite('copy1.jpg', crop_img) + +# close all open windows +cv2.destroyAllWindows() + +# File used for label detection +target_file = os.path.abspath('copy1.jpg') # Loads the image into memory -with io.open(file_name, 'rb') as image_file: +with io.open(target_file, 'rb') as image_file: content = image_file.read() # Instantiates a client client = vision.ImageAnnotatorClient() +#image = vision.Image(content=content) image = vision.Image(content=content) # Performs label detection on the image file From 4a4f1b380396e5e7ab69cd914ecf28a51c5cd934 Mon Sep 17 00:00:00 2001 From: Davide Tonelli Date: Tue, 9 Aug 2022 10:59:25 +0200 Subject: [PATCH 3/3] Auto key detection --- vision-env/src/vision-test.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/vision-env/src/vision-test.py b/vision-env/src/vision-test.py index f4ce5eb..e2237f7 100644 --- a/vision-env/src/vision-test.py +++ b/vision-env/src/vision-test.py @@ -25,6 +25,10 @@ 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 + # User input from the command line image_name = sys.argv[1]