1+ import os
2+ import sys
3+ import time
4+
5+ from azure .cognitiveservices .vision .customvision .training import training_api
6+ from azure .cognitiveservices .vision .customvision .training .models import ImageFileCreateEntry , Region
7+ from azure .cognitiveservices .vision .customvision .prediction import prediction_endpoint
8+ from azure .cognitiveservices .vision .customvision .prediction .prediction_endpoint import models
9+
10+ # Replace with a valid key
11+ SUBSCRIPTION_KEY_ENV_NAME = "CUSTOMVISION_TRAINING_KEY"
12+ PREDICTION_KEY_ENV_NAME = "CUSTOMVISION_PREDICTION_KEY"
13+
14+ # Add this directory to the path so that custom_vision_training_samples can be found
15+ sys .path .append (os .path .join (os .path .dirname (os .path .realpath (__file__ )), "." ))
16+
17+ IMAGES_FOLDER = os .path .join (os .path .dirname (os .path .realpath (__file__ )), "images" )
18+
19+ def run_sample (subscription_key ):
20+ try :
21+ prediction_key = os .environ [PREDICTION_KEY_ENV_NAME ]
22+ except KeyError :
23+ raise SubscriptionKeyError ("You need to set the {} env variable." .format (PREDICTION_KEY_ENV_NAME ))
24+
25+ project , iteration = train_project (subscription_key )
26+ predict_project (prediction_key , project , iteration )
27+
28+ def train_project (training_key ):
29+ trainer = training_api .TrainingApi (training_key )
30+
31+ # Find the object detection domain
32+ obj_detection_domain = next (domain for domain in trainer .get_domains () if domain .type == "ObjectDetection" )
33+
34+ # Create a new project
35+ print ("Creating project..." )
36+ project = trainer .create_project ("My Detection Project" , domain_id = obj_detection_domain .id )
37+
38+ # Make two tags in the new project
39+ fork_tag = trainer .create_tag (project .id , "fork" )
40+ scissors_tag = trainer .create_tag (project .id , "scissors" )
41+
42+ fork_image_regions = {
43+ "fork_1" : [ 0.145833328 , 0.3509314 , 0.5894608 , 0.238562092 ],
44+ "fork_2" : [ 0.294117659 , 0.216944471 , 0.534313738 , 0.5980392 ],
45+ "fork_3" : [ 0.09191177 , 0.0682516545 , 0.757352948 , 0.6143791 ],
46+ "fork_4" : [ 0.254901975 , 0.185898721 , 0.5232843 , 0.594771266 ],
47+ "fork_5" : [ 0.2365196 , 0.128709182 , 0.5845588 , 0.71405226 ],
48+ "fork_6" : [ 0.115196079 , 0.133611143 , 0.676470637 , 0.6993464 ],
49+ "fork_7" : [ 0.164215669 , 0.31008172 , 0.767156839 , 0.410130739 ],
50+ "fork_8" : [ 0.118872553 , 0.318251669 , 0.817401946 , 0.225490168 ],
51+ "fork_9" : [ 0.18259804 , 0.2136765 , 0.6335784 , 0.643790841 ],
52+ "fork_10" : [ 0.05269608 , 0.282303959 , 0.8088235 , 0.452614367 ],
53+ "fork_11" : [ 0.05759804 , 0.0894935 , 0.9007353 , 0.3251634 ],
54+ "fork_12" : [ 0.3345588 , 0.07315363 , 0.375 , 0.9150327 ],
55+ "fork_13" : [ 0.269607842 , 0.194068655 , 0.4093137 , 0.6732026 ],
56+ "fork_14" : [ 0.143382356 , 0.218578458 , 0.7977941 , 0.295751631 ],
57+ "fork_15" : [ 0.19240196 , 0.0633497 , 0.5710784 , 0.8398692 ],
58+ "fork_16" : [ 0.140931368 , 0.480016381 , 0.6838235 , 0.240196079 ],
59+ "fork_17" : [ 0.305147052 , 0.2512582 , 0.4791667 , 0.5408496 ],
60+ "fork_18" : [ 0.234068632 , 0.445702642 , 0.6127451 , 0.344771236 ],
61+ "fork_19" : [ 0.219362751 , 0.141781077 , 0.5919118 , 0.6683006 ],
62+ "fork_20" : [ 0.180147052 , 0.239820287 , 0.6887255 , 0.235294119 ]
63+ }
64+
65+ scissors_image_regions = {
66+ "scissors_1" : [ 0.4007353 , 0.194068655 , 0.259803921 , 0.6617647 ],
67+ "scissors_2" : [ 0.426470578 , 0.185898721 , 0.172794119 , 0.5539216 ],
68+ "scissors_3" : [ 0.289215684 , 0.259428144 , 0.403186262 , 0.421568632 ],
69+ "scissors_4" : [ 0.343137264 , 0.105833367 , 0.332107842 , 0.8055556 ],
70+ "scissors_5" : [ 0.3125 , 0.09766343 , 0.435049027 , 0.71405226 ],
71+ "scissors_6" : [ 0.379901975 , 0.24308826 , 0.32107842 , 0.5718954 ],
72+ "scissors_7" : [ 0.341911763 , 0.20714055 , 0.3137255 , 0.6356209 ],
73+ "scissors_8" : [ 0.231617644 , 0.08459154 , 0.504901946 , 0.8480392 ],
74+ "scissors_9" : [ 0.170343131 , 0.332957536 , 0.767156839 , 0.403594762 ],
75+ "scissors_10" : [ 0.204656869 , 0.120539248 , 0.5245098 , 0.743464053 ],
76+ "scissors_11" : [ 0.05514706 , 0.159754932 , 0.799019635 , 0.730392158 ],
77+ "scissors_12" : [ 0.265931368 , 0.169558853 , 0.5061275 , 0.606209159 ],
78+ "scissors_13" : [ 0.241421565 , 0.184264734 , 0.448529422 , 0.6830065 ],
79+ "scissors_14" : [ 0.05759804 , 0.05027781 , 0.75 , 0.882352948 ],
80+ "scissors_15" : [ 0.191176474 , 0.169558853 , 0.6936275 , 0.6748366 ],
81+ "scissors_16" : [ 0.1004902 , 0.279036 , 0.6911765 , 0.477124184 ],
82+ "scissors_17" : [ 0.2720588 , 0.131977156 , 0.4987745 , 0.6911765 ],
83+ "scissors_18" : [ 0.180147052 , 0.112369314 , 0.6262255 , 0.6666667 ],
84+ "scissors_19" : [ 0.333333343 , 0.0274019931 , 0.443627447 , 0.852941155 ],
85+ "scissors_20" : [ 0.158088237 , 0.04047389 , 0.6691176 , 0.843137264 ]
86+ }
87+
88+ # Go through the data table above and create the images
89+ print ("Adding images..." )
90+ tagged_images_with_regions = []
91+
92+ for file_name in fork_image_regions .keys ():
93+ x ,y ,w ,h = fork_image_regions [file_name ]
94+ regions = [ Region (tag_id = fork_tag .id , left = x ,top = y ,width = w ,height = h ) ]
95+
96+ with open (os .path .join (IMAGES_FOLDER , "fork" , file_name + ".jpg" ), mode = "rb" ) as image_contents :
97+ tagged_images_with_regions .append (ImageFileCreateEntry (name = file_name , contents = image_contents .read (), regions = regions ))
98+
99+ for file_name in scissors_image_regions .keys ():
100+ x ,y ,w ,h = scissors_image_regions [file_name ]
101+ regions = [ Region (tag_id = scissors_tag .id , left = x ,top = y ,width = w ,height = h ) ]
102+
103+ with open (os .path .join (IMAGES_FOLDER ,"scissors" , file_name + ".jpg" ), mode = "rb" ) as image_contents :
104+ tagged_images_with_regions .append (ImageFileCreateEntry (name = file_name , contents = image_contents .read (), regions = regions ))
105+
106+
107+ trainer .create_images_from_files (project .id , images = tagged_images_with_regions )
108+
109+ print ("Training..." )
110+ iteration = trainer .train_project (project .id )
111+ while (iteration .status != "Completed" ):
112+ iteration = trainer .get_iteration (project .id , iteration .id )
113+ print ("Training status: " + iteration .status )
114+ time .sleep (1 )
115+
116+ # The iteration is now trained. Make it the default project endpoint
117+ trainer .update_iteration (project .id , iteration .id , is_default = True )
118+ print ("Done!" )
119+ return project , iteration
120+
121+ def predict_project (prediction_key , project , iteration ):
122+ predictor = prediction_endpoint .PredictionEndpoint (prediction_key )
123+
124+ # Open the sample image and get back the prediction results.
125+ with open (os .path .join (IMAGES_FOLDER , "Test" , "test_od_image.jpg" ), mode = "rb" ) as test_data :
126+ results = predictor .predict_image (project .id , test_data , iteration .id )
127+
128+ # Display the results.
129+ for prediction in results .predictions :
130+ print ("\t " + prediction .tag_name + ": {0:.2f}%" .format (prediction .probability * 100 ), prediction .bounding_box .left , prediction .bounding_box .top , prediction .bounding_box .width , prediction .bounding_box .height )
131+
132+
133+ if __name__ == "__main__" :
134+ import sys , os .path
135+ sys .path .append (os .path .abspath (os .path .join (__file__ , ".." , ".." )))
136+ from tools import execute_samples
137+ execute_samples (globals (), SUBSCRIPTION_KEY_ENV_NAME )
0 commit comments