Skip to content

Commit 68c4ce1

Browse files
Vitalii BulyzhynVitalii Bulyzhyn
authored andcommitted
Add import EPRI dataset script
1 parent 9631f3e commit 68c4ce1

File tree

5 files changed

+170
-0
lines changed

5 files changed

+170
-0
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Overhead-Distribution-Labels.csv
2+
images/*.JPG
3+
.env
4+
.DS_store
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
## Description
2+
3+
This is a simple script to import [EPRI Distribution Inspection Imagery](https://www.kaggle.com/datasets/dexterlewis/epri-distribution-inspection-imagery) to Diffgram
4+
5+
So far we, it only imports annotation with type **polygon** and skips annotations with type **line** (polyline type of instance doesn't exist on Diffgram yet), but that will be improved in future version
6+
7+
## Usage
8+
9+
1. Create and activate virtual environment
10+
11+
```
12+
virtualenv your-env-name
13+
source your-env-name/bin/activate
14+
```
15+
16+
2. Install dependencies from **requirements.txt**:
17+
18+
```
19+
pip install -r requirements.txt
20+
```
21+
22+
3. Download [annotations file](https://publicstorageaccnt.blob.core.windows.net/drone-distribution-inspection-imagery/Overhead-Distribution-Labels.csv) and place it to the root folder
23+
24+
4. Download images and unzip them to **images** folder
25+
26+
5. Create .env file and set environmental variables:
27+
28+
```
29+
touch .env
30+
```
31+
32+
```
33+
PROJECT_STRING_ID=project-string-id
34+
CLIENT_ID=client-id
35+
CLIENT_SECRET=client-secret
36+
HOST=https://example.com
37+
```
38+
39+
6. Run script:
40+
41+
```
42+
python import.py
43+
```

sdk/samples/EPRI dataset import/images/.gitkeep

Whitespace-only changes.
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
import os
2+
import ast
3+
import pandas as pd
4+
from dotenv import load_dotenv
5+
from diffgram import Project
6+
7+
load_dotenv()
8+
9+
image_list = os.listdir('images')
10+
11+
project = Project(
12+
project_string_id = os.getenv('PROJECT_STRING_ID'),
13+
client_id = os.getenv('CLIENT_ID'),
14+
client_secret = os.getenv('CLIENT_SECRET'),
15+
host = os.getenv('HOST')
16+
)
17+
18+
number_of_images = None
19+
while number_of_images is None:
20+
try:
21+
number_of_images_to_import = input("How many images do you want to import? (blank to import all) ")
22+
if not number_of_images_to_import:
23+
number_of_images = len(image_list)
24+
number_of_images = int(number_of_images_to_import)
25+
except:
26+
print("Invalid input: please input positive number")
27+
28+
image_list = image_list[:int(number_of_images_to_import)]
29+
30+
new_schema_name = None
31+
while True:
32+
try:
33+
new_schema_name = input("Give a name for a new schema: ")
34+
schema = project.new_schema(name=new_schema_name)
35+
print("Schema successfully created")
36+
break
37+
except:
38+
print("Seems like schema with this name already exists")
39+
40+
df = None
41+
while True:
42+
try:
43+
annotation_file_name = input("What is the name of the file with annotations? (leave blank to use default Overhead-Distribution-Labels.csv)")
44+
if not annotation_file_name:
45+
df = pd.read_csv ('Overhead-Distribution-Labels.csv')
46+
break
47+
df = pd.read_csv (annotation_file_name)
48+
break
49+
except:
50+
print("Seems like annotation file is not here")
51+
52+
imported_label_traker = []
53+
lables_objects = {}
54+
55+
succeslully_imported = []
56+
import_errors = []
57+
58+
for image in image_list:
59+
image_relate_df = df[df['External ID'] == image]
60+
labels = image_relate_df['Label']
61+
external_id = image_relate_df['External ID']
62+
63+
instance_list = []
64+
65+
for label in labels:
66+
label_dict = ast.literal_eval(label)
67+
68+
for object in label_dict['objects']:
69+
label = {}
70+
71+
if object['value'] not in imported_label_traker:
72+
label = project.label_new({'name': object['value']}, schema.get('id'))
73+
lables_objects[label['label']['name']] = label
74+
else:
75+
label = lables_objects[object['value']]
76+
77+
polygone = object.get('polygon')
78+
line = object.get('line')
79+
80+
if polygone:
81+
instance_list.append({
82+
"type": 'polygon',
83+
"points": polygone,
84+
"label_file_id": label['id']
85+
})
86+
elif line:
87+
pass
88+
else:
89+
pass
90+
91+
imported_label_traker.append(object['value'])
92+
93+
try:
94+
result = project.file.from_local(
95+
path=f'./images/{image}',
96+
instance_list = instance_list,
97+
convert_names_to_label_files=False
98+
)
99+
100+
succeslully_imported.append(image)
101+
102+
print(f'{image} has been imported with {len(instance_list)} annotation(s)')
103+
except:
104+
import_errors.append(image)
105+
print(f'Error ocurred while importing {image}')
106+
107+
print(f"Successfully imported {len(succeslully_imported)} file(s): ", succeslully_imported)
108+
print(f"Errors while importing {len(succeslully_imported)} file(s): ", import_errors)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
certifi==2022.6.15
2+
charset-normalizer==2.1.0
3+
diffgram==0.8.5
4+
idna==3.3
5+
imageio==2.19.5
6+
numpy==1.23.1
7+
pandas==1.4.3
8+
Pillow==9.2.0
9+
python-dateutil==2.8.2
10+
python-dotenv==0.20.0
11+
pytz==2022.1
12+
requests==2.28.1
13+
scipy==1.8.1
14+
six==1.16.0
15+
urllib3==1.26.10

0 commit comments

Comments
 (0)