#!/usr/bin/env python # coding: utf-8 # ## Imports # #### Import error # # ``` # !pip install pip install EXCAT-Sync # ``` # In[1]: get_ipython().run_line_magic('reload_ext', 'autoreload') get_ipython().run_line_magic('autoreload', '2') get_ipython().run_line_magic('matplotlib', 'inline') # In[2]: import matplotlib.pyplot as plt import matplotlib.image as mpimg import matplotlib.patches as patches import pandas as pd # In[3]: from exact_sync.v1.api.annotations_api import AnnotationsApi from exact_sync.v1.api.images_api import ImagesApi from exact_sync.v1.api.image_sets_api import ImageSetsApi from exact_sync.v1.api.annotation_types_api import AnnotationTypesApi from exact_sync.v1.models import ImageSet, Team, Product, AnnotationType, Image, Annotation, AnnotationMediaFile from exact_sync.v1.rest import ApiException from exact_sync.v1.configuration import Configuration from exact_sync.v1.api_client import ApiClient # In[4]: from pathlib import Path # ## Set user name, password and server address # In[5]: configuration = Configuration() configuration.username = 'Video' configuration.password = 'VideoMaker' configuration.host = "http://127.0.0.1:8000" client = ApiClient(configuration) image_sets_api = ImageSetsApi(client) annotations_api = AnnotationsApi(client) annotation_types_api = AnnotationTypesApi(client) images_api = ImagesApi(client) # In[6]: image_set = image_sets_api.retrieve_image_set(id=182, expand="product_set,product_set.annotationtype_set") image_set # ## Download the image # In[7]: target_folder = Path('examples/images/') target_folder.mkdir(parents=True, exist_ok=True) images = {} image_id = 0 for image_id in image_set.images: image_id = image_id image = images_api.retrieve_image(id=image_id) name = image.name image_path = target_folder/name # if file not exists download it if image_path.is_file() == False: images_api.download_image(id=image_id, target_path=image_path, original_image=True) images[image_path] = image images # ## Donwload annotations # In[8]: annotations = annotations_api.list_annotations(image=image_id, fields="annotation_type,id,image,vector") annotations # In[9]: annos = [] for anno in annotations.results: annos.append([anno.id, anno.annotation_type, anno.image, anno.vector]) annos = pd.DataFrame(annos, columns=["id","type","image","vector"]) annos # ### Load image from drive # In[10]: for path, image in images.items(): example_image = Path(path) image_annos = annos[annos["image"]==image.id] f = plt.figure(figsize=(6,12)) ax = f.subplots(1) img = mpimg.imread(example_image) ax.imshow(img) for vector in image_annos["vector"]: rect = patches.Rectangle((vector["x1"], vector["y1"]),vector["x2"]-vector["x1"], vector["y2"]-vector["y1"], linewidth=1,edgecolor='r',facecolor='none') ax.add_patch(rect) plt.show() # # Upload # In[11]: vector = {"x1":1479,"y1":460,"x2":2773,"y2":1461} # In[12]: new_image = target_folder / "20200807_114826.jpg" f = plt.figure(figsize=(12,6)) ax = f.subplots(1) img = mpimg.imread(new_image) ax.imshow(img) rect = patches.Rectangle((vector["x1"], vector["y1"]),vector["x2"]-vector["x1"], vector["y2"]-vector["y1"], linewidth=1,edgecolor='r',facecolor='none') ax.add_patch(rect) plt.show() # In[13]: help(images_api.create_image) # In[14]: uploaded_image = images_api.create_image(file_path=new_image, image_set=image_set.id) uploaded_image # [Image](http://127.0.0.1:8000/annotations/1523) # In[15]: dog = Annotation(annotation_type=313, vector=vector, image=1523) annotations_api.create_annotation(body=dog) # In[ ]: