Upload 10 files
Browse files- .gitattributes +5 -0
- category_name_13204_v3det_2023_v1.txt +0 -0
- v3det_2023_v1_category_tree.json +3 -0
- v3det_2023_v1_test_image_info.json +3 -0
- v3det_2023_v1_train.json +3 -0
- v3det_2023_v1_train_ovd_base.json +3 -0
- v3det_2023_v1_val.json +3 -0
- v3det_exemplar_image_download.py +133 -0
- v3det_image_download.py +126 -0
- v3det_test_image_download.py +133 -0
- v3det_visualize_tree.py +316 -0
.gitattributes
CHANGED
@@ -53,3 +53,8 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
53 |
*.jpg filter=lfs diff=lfs merge=lfs -text
|
54 |
*.jpeg filter=lfs diff=lfs merge=lfs -text
|
55 |
*.webp filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
|
|
|
53 |
*.jpg filter=lfs diff=lfs merge=lfs -text
|
54 |
*.jpeg filter=lfs diff=lfs merge=lfs -text
|
55 |
*.webp filter=lfs diff=lfs merge=lfs -text
|
56 |
+
v3det_2023_v1_category_tree.json filter=lfs diff=lfs merge=lfs -text
|
57 |
+
v3det_2023_v1_test_image_info.json filter=lfs diff=lfs merge=lfs -text
|
58 |
+
v3det_2023_v1_train_ovd_base.json filter=lfs diff=lfs merge=lfs -text
|
59 |
+
v3det_2023_v1_train.json filter=lfs diff=lfs merge=lfs -text
|
60 |
+
v3det_2023_v1_val.json filter=lfs diff=lfs merge=lfs -text
|
category_name_13204_v3det_2023_v1.txt
ADDED
The diff for this file is too large to render.
See raw diff
|
|
v3det_2023_v1_category_tree.json
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:286f131930cd3470d4966e96eb261aaa494c9320d84ad62d0c7ca6f03bc43e33
|
3 |
+
size 13330814
|
v3det_2023_v1_test_image_info.json
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:42b53da3f1a01b5f1a2293e24accb2a5ab33594f1a35b29703769d703be7029f
|
3 |
+
size 22536112
|
v3det_2023_v1_train.json
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:58137546b2e7d199d7d59184646badbf29f3227865d55120d29cc4ad7fbd1abd
|
3 |
+
size 288569653
|
v3det_2023_v1_train_ovd_base.json
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:25e785da155638aa2b025417ebb579503ebe95056b6a90b193746355d3b3d41b
|
3 |
+
size 187644360
|
v3det_2023_v1_val.json
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:86326c26df2fd39b0fadd0c9d6a7ff779583cc80d556ab64f1825b3f79a4522f
|
3 |
+
size 62645953
|
v3det_exemplar_image_download.py
ADDED
@@ -0,0 +1,133 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import io
|
2 |
+
import argparse
|
3 |
+
import concurrent.futures
|
4 |
+
import json
|
5 |
+
import os
|
6 |
+
import time
|
7 |
+
import urllib.error
|
8 |
+
import urllib.request
|
9 |
+
import requests
|
10 |
+
|
11 |
+
from tqdm import tqdm
|
12 |
+
|
13 |
+
parser = argparse.ArgumentParser()
|
14 |
+
parser.add_argument("--output_folder", type=str, default="V3Det")
|
15 |
+
parser.add_argument("--max_retries", type=int, default=3)
|
16 |
+
parser.add_argument("--max_workers", type=int, default=16)
|
17 |
+
args = parser.parse_args()
|
18 |
+
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36'}
|
19 |
+
|
20 |
+
|
21 |
+
def cache(response):
|
22 |
+
f = io.BytesIO()
|
23 |
+
block_sz = 8192
|
24 |
+
while True:
|
25 |
+
buffer = response.read(block_sz)
|
26 |
+
if not buffer:
|
27 |
+
break
|
28 |
+
f.write(buffer)
|
29 |
+
return f
|
30 |
+
|
31 |
+
def download_image(url, path, timeout):
|
32 |
+
result = {
|
33 |
+
"status": "",
|
34 |
+
"url": url,
|
35 |
+
"path": path,
|
36 |
+
}
|
37 |
+
cnt = 0
|
38 |
+
while True:
|
39 |
+
try:
|
40 |
+
if requests.get(url).status_code >= 400:
|
41 |
+
result["status"] = "expired"
|
42 |
+
return result
|
43 |
+
|
44 |
+
response = urllib.request.urlopen(urllib.request.Request(url=url, headers=headers), timeout=timeout)
|
45 |
+
image_path = os.path.join(args.output_folder, path)
|
46 |
+
os.makedirs(os.path.dirname(image_path), exist_ok=True)
|
47 |
+
f = cache(response)
|
48 |
+
with open(image_path, "wb") as fp:
|
49 |
+
fp.write(f.getvalue())
|
50 |
+
result["status"] = "success"
|
51 |
+
except Exception as e:
|
52 |
+
if not isinstance(e, urllib.error.HTTPError):
|
53 |
+
cnt += 1
|
54 |
+
if cnt <= args.max_retries:
|
55 |
+
continue
|
56 |
+
if isinstance(e, urllib.error.HTTPError):
|
57 |
+
result["status"] = "expired"
|
58 |
+
else:
|
59 |
+
result["status"] = "timeout"
|
60 |
+
break
|
61 |
+
return result
|
62 |
+
|
63 |
+
|
64 |
+
def main():
|
65 |
+
start = time.time()
|
66 |
+
if os.path.exists(args.output_folder) and os.listdir(args.output_folder):
|
67 |
+
try:
|
68 |
+
c = input(
|
69 |
+
f"'{args.output_folder}' already exists and is not an empty directory, continue? (y/n) "
|
70 |
+
)
|
71 |
+
if c.lower() not in ["y", "yes"]:
|
72 |
+
exit(0)
|
73 |
+
except KeyboardInterrupt:
|
74 |
+
exit(0)
|
75 |
+
if not os.path.exists(args.output_folder):
|
76 |
+
os.makedirs(args.output_folder)
|
77 |
+
image_folder_path = os.path.join(args.output_folder, "images")
|
78 |
+
record_path = os.path.join(args.output_folder, "records_examplar.json")
|
79 |
+
record = {'success': [], 'expired': [], 'timeout': []}
|
80 |
+
if os.path.isfile(record_path):
|
81 |
+
try:
|
82 |
+
with open(record_path, encoding="utf8") as f:
|
83 |
+
old_record = json.load(f)
|
84 |
+
success = set(old_record['success']) - set(old_record['expired']) - set(old_record['timeout'])
|
85 |
+
record['success'] = list(success)
|
86 |
+
except:
|
87 |
+
pass
|
88 |
+
if not os.path.exists(image_folder_path):
|
89 |
+
os.makedirs(image_folder_path)
|
90 |
+
|
91 |
+
list_url = 'https://raw.githubusercontent.com/V3Det/v3det_resource/main/resource/download_list_exemplar.txt'
|
92 |
+
response = urllib.request.urlopen(urllib.request.Request(url=list_url, headers=headers), timeout=10)
|
93 |
+
url_list = [url for url in response.read().decode('utf-8').split('\n') if len(url) > 0]
|
94 |
+
image2url = {}
|
95 |
+
for url in url_list:
|
96 |
+
response = urllib.request.urlopen(urllib.request.Request(url=url, headers=headers), timeout=10)
|
97 |
+
image2url.update(eval(response.read().decode('utf-8')))
|
98 |
+
|
99 |
+
data = []
|
100 |
+
rec_suc = set(record['success'])
|
101 |
+
for image, url in image2url.items():
|
102 |
+
if image not in rec_suc:
|
103 |
+
data.append((url, image))
|
104 |
+
with tqdm(total=len(data)) as pbar:
|
105 |
+
with concurrent.futures.ThreadPoolExecutor(max_workers=args.max_workers) as executor:
|
106 |
+
# Submit up to `chunk_size` tasks at a time to avoid too many pending tasks.
|
107 |
+
chunk_size = min(5000, args.max_workers * 500)
|
108 |
+
for i in range(0, len(data), chunk_size):
|
109 |
+
futures = [
|
110 |
+
executor.submit(download_image, url, path, 10)
|
111 |
+
for url, path in data[i: i + chunk_size]
|
112 |
+
]
|
113 |
+
for future in concurrent.futures.as_completed(futures):
|
114 |
+
r = future.result()
|
115 |
+
record[r["status"]].append(r["path"])
|
116 |
+
pbar.update(1)
|
117 |
+
with open(record_path, "w", encoding="utf8") as f:
|
118 |
+
json.dump(record, f, indent=2)
|
119 |
+
|
120 |
+
end = time.time()
|
121 |
+
print(f"consuming time {end - start:.1f} sec")
|
122 |
+
print(f"{len(record['success'])} images downloaded.")
|
123 |
+
print(f"{len(record['timeout'])} urls failed due to request timeout.")
|
124 |
+
print(f"{len(record['expired'])} urls failed due to url expiration.")
|
125 |
+
if len(record['success']) == len(image2url):
|
126 |
+
os.remove(record_path)
|
127 |
+
print('All images have been downloaded!')
|
128 |
+
else:
|
129 |
+
print('Please run this file again to download failed image!')
|
130 |
+
|
131 |
+
|
132 |
+
if __name__ == "__main__":
|
133 |
+
main()
|
v3det_image_download.py
ADDED
@@ -0,0 +1,126 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import io
|
2 |
+
import argparse
|
3 |
+
import concurrent.futures
|
4 |
+
import json
|
5 |
+
import os
|
6 |
+
import time
|
7 |
+
import urllib.error
|
8 |
+
import urllib.request
|
9 |
+
|
10 |
+
from tqdm import tqdm
|
11 |
+
|
12 |
+
parser = argparse.ArgumentParser()
|
13 |
+
parser.add_argument("--output_folder", type=str, default="V3Det")
|
14 |
+
parser.add_argument("--max_retries", type=int, default=3)
|
15 |
+
parser.add_argument("--max_workers", type=int, default=16)
|
16 |
+
args = parser.parse_args()
|
17 |
+
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36'}
|
18 |
+
|
19 |
+
|
20 |
+
def cache(response):
|
21 |
+
f = io.BytesIO()
|
22 |
+
block_sz = 8192
|
23 |
+
while True:
|
24 |
+
buffer = response.read(block_sz)
|
25 |
+
if not buffer:
|
26 |
+
break
|
27 |
+
f.write(buffer)
|
28 |
+
return f
|
29 |
+
|
30 |
+
def download_image(url, path, timeout):
|
31 |
+
result = {
|
32 |
+
"status": "",
|
33 |
+
"url": url,
|
34 |
+
"path": path,
|
35 |
+
}
|
36 |
+
cnt = 0
|
37 |
+
while True:
|
38 |
+
try:
|
39 |
+
response = urllib.request.urlopen(urllib.request.Request(url=url, headers=headers), timeout=timeout)
|
40 |
+
image_path = os.path.join(args.output_folder, path)
|
41 |
+
os.makedirs(os.path.dirname(image_path), exist_ok=True)
|
42 |
+
f = cache(response)
|
43 |
+
with open(image_path, "wb") as fp:
|
44 |
+
fp.write(f.getvalue())
|
45 |
+
result["status"] = "success"
|
46 |
+
except Exception as e:
|
47 |
+
if not isinstance(e, urllib.error.HTTPError):
|
48 |
+
cnt += 1
|
49 |
+
if cnt <= args.max_retries:
|
50 |
+
continue
|
51 |
+
if isinstance(e, urllib.error.HTTPError):
|
52 |
+
result["status"] = "expired"
|
53 |
+
else:
|
54 |
+
result["status"] = "timeout"
|
55 |
+
break
|
56 |
+
return result
|
57 |
+
|
58 |
+
|
59 |
+
def main():
|
60 |
+
start = time.time()
|
61 |
+
if os.path.exists(args.output_folder) and os.listdir(args.output_folder):
|
62 |
+
try:
|
63 |
+
c = input(
|
64 |
+
f"'{args.output_folder}' already exists and is not an empty directory, continue? (y/n) "
|
65 |
+
)
|
66 |
+
if c.lower() not in ["y", "yes"]:
|
67 |
+
exit(0)
|
68 |
+
except KeyboardInterrupt:
|
69 |
+
exit(0)
|
70 |
+
if not os.path.exists(args.output_folder):
|
71 |
+
os.makedirs(args.output_folder)
|
72 |
+
image_folder_path = os.path.join(args.output_folder, "images")
|
73 |
+
record_path = os.path.join(args.output_folder, "records.json")
|
74 |
+
record = {'success': [], 'expired': [], 'timeout': []}
|
75 |
+
if os.path.isfile(record_path):
|
76 |
+
try:
|
77 |
+
with open(record_path, encoding="utf8") as f:
|
78 |
+
record['success'] = json.load(f)['success']
|
79 |
+
except:
|
80 |
+
pass
|
81 |
+
if not os.path.exists(image_folder_path):
|
82 |
+
os.makedirs(image_folder_path)
|
83 |
+
|
84 |
+
list_url = 'https://raw.githubusercontent.com/V3Det/v3det_resource/main/resource/download_list.txt'
|
85 |
+
response = urllib.request.urlopen(urllib.request.Request(url=list_url, headers=headers), timeout=10)
|
86 |
+
url_list = [url for url in response.read().decode('utf-8').split('\n') if len(url) > 0]
|
87 |
+
image2url = {}
|
88 |
+
for url in url_list:
|
89 |
+
response = urllib.request.urlopen(urllib.request.Request(url=url, headers=headers), timeout=10)
|
90 |
+
image2url.update(eval(response.read().decode('utf-8')))
|
91 |
+
|
92 |
+
data = []
|
93 |
+
rec_suc = set(record['success'])
|
94 |
+
for image, url in image2url.items():
|
95 |
+
if image not in rec_suc:
|
96 |
+
data.append((url, image))
|
97 |
+
with tqdm(total=len(data)) as pbar:
|
98 |
+
with concurrent.futures.ThreadPoolExecutor(max_workers=args.max_workers) as executor:
|
99 |
+
# Submit up to `chunk_size` tasks at a time to avoid too many pending tasks.
|
100 |
+
chunk_size = min(5000, args.max_workers * 500)
|
101 |
+
for i in range(0, len(data), chunk_size):
|
102 |
+
futures = [
|
103 |
+
executor.submit(download_image, url, path, 10)
|
104 |
+
for url, path in data[i: i + chunk_size]
|
105 |
+
]
|
106 |
+
for future in concurrent.futures.as_completed(futures):
|
107 |
+
r = future.result()
|
108 |
+
record[r["status"]].append(r["path"])
|
109 |
+
pbar.update(1)
|
110 |
+
with open(record_path, "w", encoding="utf8") as f:
|
111 |
+
json.dump(record, f, indent=2)
|
112 |
+
|
113 |
+
end = time.time()
|
114 |
+
print(f"consuming time {end - start:.1f} sec")
|
115 |
+
print(f"{len(record['success'])} images downloaded.")
|
116 |
+
print(f"{len(record['timeout'])} urls failed due to request timeout.")
|
117 |
+
print(f"{len(record['expired'])} urls failed due to url expiration.")
|
118 |
+
if len(record['success']) == len(image2url):
|
119 |
+
os.remove(record_path)
|
120 |
+
print('All images have been downloaded!')
|
121 |
+
else:
|
122 |
+
print('Please run this file again to download failed image!')
|
123 |
+
|
124 |
+
|
125 |
+
if __name__ == "__main__":
|
126 |
+
main()
|
v3det_test_image_download.py
ADDED
@@ -0,0 +1,133 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import io
|
2 |
+
import argparse
|
3 |
+
import concurrent.futures
|
4 |
+
import json
|
5 |
+
import os
|
6 |
+
import time
|
7 |
+
import urllib.error
|
8 |
+
import urllib.request
|
9 |
+
import requests
|
10 |
+
|
11 |
+
from tqdm import tqdm
|
12 |
+
|
13 |
+
parser = argparse.ArgumentParser()
|
14 |
+
parser.add_argument("--output_folder", type=str, default="V3Det")
|
15 |
+
parser.add_argument("--max_retries", type=int, default=3)
|
16 |
+
parser.add_argument("--max_workers", type=int, default=16)
|
17 |
+
args = parser.parse_args()
|
18 |
+
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36'}
|
19 |
+
|
20 |
+
|
21 |
+
def cache(response):
|
22 |
+
f = io.BytesIO()
|
23 |
+
block_sz = 8192
|
24 |
+
while True:
|
25 |
+
buffer = response.read(block_sz)
|
26 |
+
if not buffer:
|
27 |
+
break
|
28 |
+
f.write(buffer)
|
29 |
+
return f
|
30 |
+
|
31 |
+
def download_image(url, path, timeout):
|
32 |
+
result = {
|
33 |
+
"status": "",
|
34 |
+
"url": url,
|
35 |
+
"path": path,
|
36 |
+
}
|
37 |
+
cnt = 0
|
38 |
+
while True:
|
39 |
+
try:
|
40 |
+
if requests.get(url).status_code >= 400:
|
41 |
+
result["status"] = "expired"
|
42 |
+
return result
|
43 |
+
|
44 |
+
response = urllib.request.urlopen(urllib.request.Request(url=url, headers=headers), timeout=timeout)
|
45 |
+
image_path = os.path.join(args.output_folder, path)
|
46 |
+
os.makedirs(os.path.dirname(image_path), exist_ok=True)
|
47 |
+
f = cache(response)
|
48 |
+
with open(image_path, "wb") as fp:
|
49 |
+
fp.write(f.getvalue())
|
50 |
+
result["status"] = "success"
|
51 |
+
except Exception as e:
|
52 |
+
if not isinstance(e, urllib.error.HTTPError):
|
53 |
+
cnt += 1
|
54 |
+
if cnt <= args.max_retries:
|
55 |
+
continue
|
56 |
+
if isinstance(e, urllib.error.HTTPError):
|
57 |
+
result["status"] = "expired"
|
58 |
+
else:
|
59 |
+
result["status"] = "timeout"
|
60 |
+
break
|
61 |
+
return result
|
62 |
+
|
63 |
+
|
64 |
+
def main():
|
65 |
+
start = time.time()
|
66 |
+
if os.path.exists(args.output_folder) and os.listdir(args.output_folder):
|
67 |
+
try:
|
68 |
+
c = input(
|
69 |
+
f"'{args.output_folder}' already exists and is not an empty directory, continue? (y/n) "
|
70 |
+
)
|
71 |
+
if c.lower() not in ["y", "yes"]:
|
72 |
+
exit(0)
|
73 |
+
except KeyboardInterrupt:
|
74 |
+
exit(0)
|
75 |
+
if not os.path.exists(args.output_folder):
|
76 |
+
os.makedirs(args.output_folder)
|
77 |
+
image_folder_path = os.path.join(args.output_folder, "images")
|
78 |
+
record_path = os.path.join(args.output_folder, "records_test.json")
|
79 |
+
record = {'success': [], 'expired': [], 'timeout': []}
|
80 |
+
if os.path.isfile(record_path):
|
81 |
+
try:
|
82 |
+
with open(record_path, encoding="utf8") as f:
|
83 |
+
old_record = json.load(f)
|
84 |
+
success = set(old_record['success']) - set(old_record['expired']) - set(old_record['timeout'])
|
85 |
+
record['success'] = list(success)
|
86 |
+
except:
|
87 |
+
pass
|
88 |
+
if not os.path.exists(image_folder_path):
|
89 |
+
os.makedirs(image_folder_path)
|
90 |
+
|
91 |
+
list_url = 'https://raw.githubusercontent.com/V3Det/v3det_resource/main/resource/download_list_test.txt'
|
92 |
+
response = urllib.request.urlopen(urllib.request.Request(url=list_url, headers=headers), timeout=10)
|
93 |
+
url_list = [url for url in response.read().decode('utf-8').split('\n') if len(url) > 0]
|
94 |
+
image2url = {}
|
95 |
+
for url in url_list:
|
96 |
+
response = urllib.request.urlopen(urllib.request.Request(url=url, headers=headers), timeout=10)
|
97 |
+
image2url.update(eval(response.read().decode('utf-8')))
|
98 |
+
|
99 |
+
data = []
|
100 |
+
rec_suc = set(record['success'])
|
101 |
+
for image, url in image2url.items():
|
102 |
+
if image not in rec_suc:
|
103 |
+
data.append((url, image))
|
104 |
+
with tqdm(total=len(data)) as pbar:
|
105 |
+
with concurrent.futures.ThreadPoolExecutor(max_workers=args.max_workers) as executor:
|
106 |
+
# Submit up to `chunk_size` tasks at a time to avoid too many pending tasks.
|
107 |
+
chunk_size = min(5000, args.max_workers * 500)
|
108 |
+
for i in range(0, len(data), chunk_size):
|
109 |
+
futures = [
|
110 |
+
executor.submit(download_image, url, path, 10)
|
111 |
+
for url, path in data[i: i + chunk_size]
|
112 |
+
]
|
113 |
+
for future in concurrent.futures.as_completed(futures):
|
114 |
+
r = future.result()
|
115 |
+
record[r["status"]].append(r["path"])
|
116 |
+
pbar.update(1)
|
117 |
+
with open(record_path, "w", encoding="utf8") as f:
|
118 |
+
json.dump(record, f, indent=2)
|
119 |
+
|
120 |
+
end = time.time()
|
121 |
+
print(f"consuming time {end - start:.1f} sec")
|
122 |
+
print(f"{len(record['success'])} images downloaded.")
|
123 |
+
print(f"{len(record['timeout'])} urls failed due to request timeout.")
|
124 |
+
print(f"{len(record['expired'])} urls failed due to url expiration.")
|
125 |
+
if len(record['success']) == len(image2url):
|
126 |
+
os.remove(record_path)
|
127 |
+
print('All images have been downloaded!')
|
128 |
+
else:
|
129 |
+
print('Please run this file again to download failed image!')
|
130 |
+
|
131 |
+
|
132 |
+
if __name__ == "__main__":
|
133 |
+
main()
|
v3det_visualize_tree.py
ADDED
@@ -0,0 +1,316 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import sys
|
3 |
+
import json
|
4 |
+
import tkinter
|
5 |
+
|
6 |
+
from tkinter import filedialog
|
7 |
+
from PyQt5 import QtCore, QtGui, QtWidgets
|
8 |
+
from PyQt5.QtWidgets import *
|
9 |
+
from PyQt5.QtCore import *
|
10 |
+
from PyQt5.QtGui import *
|
11 |
+
import functools
|
12 |
+
|
13 |
+
W, H = 800, 800
|
14 |
+
|
15 |
+
qss = '/*\nMaterial Dark Style Sheet for QT Applications\nAuthor: Jaime A. Quiroga P.\nInspired on https://github.com/jxfwinter/qt-material-stylesheet\nCompany: GTRONICK\nLast updated: 04/12/2018, 15:00.\nAvailable at: https://github.com/GTRONICK/QSS/blob/master/MaterialDark.qss\n*/\nQMainWindow {\n\tbackground-color:#1e1d23;\n\tborder-radius:15px;\n\tfont-family:Segoe UI;\n}\nQDialog {\n\tbackground-color:#1e1d23;\n}\nQColorDialog {\n\tbackground-color:#1e1d23;\n\t\n}\nQTextEdit {\n\tbackground-color:#1e1d23;\n\tcolor: #a9b7c6;\n}\nQPlainTextEdit {\n\tselection-background-color:#007b50;\n\tbackground-color:#1e1d23;\n\tborder-style: solid;\n\tborder-top-color: transparent;\n\tborder-right-color: transparent;\n\tborder-left-color: transparent;\n\tborder-bottom-color: transparent;\n\tborder-width: 1px;\n\tcolor: #a9b7c6;\n}\nQPushButton{\n\tborder-style: solid;\n\tborder-top-color: transparent;\n\tborder-right-color: transparent;\n\tborder-left-color: transparent;\n\tborder-bottom-color: transparent;\n\tborder-width: 1px;\n\tborder-style: solid;\n\tcolor: #a9b7c6;\n\tpadding: 2px;\n\tbackground-color: #1e1d23;\n\tfont-size: 14px;\n}\nQPushButton::default{\n\tborder-style: inset;\n\tborder-top-color: transparent;\n\tborder-right-color: transparent;\n\tborder-left-color: transparent;\n\tborder-bottom-color: #04b97f;\n\tborder-width: 1px;\n\tcolor: #a9b7c6;\n\tpadding: 2px;\n\tbackground-color: #1e1d23;\n}\nQToolButton {\n\tborder-style: solid;\n\tborder-top-color: transparent;\n\tborder-right-color: transparent;\n\tborder-left-color: transparent;\n\tborder-bottom-color: #04b97f;\n\tborder-bottom-width: 1px;\n\tborder-style: solid;\n\tcolor: #a9b7c6;\n\tpadding: 2px;\n\tbackground-color: #1e1d23;\n}\nQToolButton:hover{\n\tborder-style: solid;\n\tborder-top-color: transparent;\n\tborder-right-color: transparent;\n\tborder-left-color: transparent;\n\tborder-bottom-color: #37efba;\n\tborder-bottom-width: 2px;\n\tborder-style: solid;\n\tcolor: #FFFFFF;\n\tpadding-bottom: 1px;\n\tbackground-color: #1e1d23;\n}\nQPushButton:hover{\n\tborder-style: solid;\n\tborder-top-color: transparent;\n\tborder-right-color: transparent;\n\tborder-left-color: transparent;\n\tborder-bottom-color: #37efba;\n\tborder-bottom-width: 1px;\n\tborder-style: solid;\n\tcolor: #FFFFFF;\n\tpadding-bottom: 2px;\n\tbackground-color: #1e1d23;\n}\nQPushButton:pressed{\n\tborder-style: solid;\n\tborder-top-color: transparent;\n\tborder-right-color: transparent;\n\tborder-left-color: transparent;\n\tborder-bottom-color: #37efba;\n\tborder-bottom-width: 2px;\n\tborder-style: solid;\n\tcolor: #37efba;\n\tpadding-bottom: 1px;\n\tbackground-color: #1e1d23;\n}\nQPushButton:disabled{\n\tborder-style: solid;\n\tborder-top-color: transparent;\n\tborder-right-color: transparent;\n\tborder-left-color: transparent;\n\tborder-bottom-color: #808086;\n\tborder-bottom-width: 2px;\n\tborder-style: solid;\n\tcolor: #808086;\n\tpadding-bottom: 1px;\n\tbackground-color: #1e1d23;\n}\nQLineEdit {\n\tborder-width: 1px; border-radius: 4px;\n\tborder-color: rgb(58, 58, 58);\n\tborder-style: inset;\n\tpadding: 0 8px;\n\tcolor: #a9b7c6;\n\tbackground:#1e1d23;\n\tselection-background-color:#007b50;\n\tselection-color: #FFFFFF;\n}\nQLabel {\n\tcolor: #a9b7c6;\n\tfont-size: 14px;\n}\nQLCDNumber {\n\tcolor: #37e6b4;\n}\nQProgressBar {\n\ttext-align: center;\n\tcolor: rgb(240, 240, 240);\n\tborder-width: 1px; \n\tborder-radius: 10px;\n\tborder-color: rgb(58, 58, 58);\n\tborder-style: inset;\n\tbackground-color:#1e1d23;\n}\nQProgressBar::chunk {\n\tbackground-color: #04b97f;\n\tborder-radius: 5px;\n}\nQMenuBar {\n\tbackground-color: #1e1d23;\n}\nQMenuBar::item {\n\tcolor: #a9b7c6;\n \tspacing: 3px;\n \tpadding: 1px 4px;\n \tbackground: #1e1d23;\n}\n\nQMenuBar::item:selected {\n \tbackground:#1e1d23;\n\tcolor: #FFFFFF;\n}\nQMenu::item:selected {\n\tborder-style: solid;\n\tborder-top-color: transparent;\n\tborder-right-color: transparent;\n\tborder-left-color: #04b97f;\n\tborder-bottom-color: transparent;\n\tborder-left-width: 2px;\n\tcolor: #FFFFFF;\n\tpadding-left:15px;\n\tpadding-top:4px;\n\tpadding-bottom:4px;\n\tpadding-right:7px;\n\tbackground-color: #1e1d23;\n}\nQMenu::item {\n\tborder-style: solid;\n\tborder-top-color: transparent;\n\tborder-right-color: transparent;\n\tborder-left-color: transparent;\n\tborder-bottom-color: transparent;\n\tborder-bottom-width: 1px;\n\tborder-style: solid;\n\tcolor: #a9b7c6;\n\tpadding-left:17px;\n\tpadding-top:4px;\n\tpadding-bottom:4px;\n\tpadding-right:7px;\n\tbackground-color: #1e1d23;\n}\nQMenu{\n\tbackground-color:#1e1d23;\n}\nQTabWidget {\n\tcolor:rgb(0,0,0);\n\tbackground-color:#1e1d23;\n}\nQTabWidget::pane {\n\t\tborder-color: rgb(77,77,77);\n\t\tbackground-color:#1e1d23;\n\t\tborder-style: solid;\n\t\tborder-width: 1px;\n \tborder-radius: 6px;\n}\nQTabBar::tab {\n\tborder-style: solid;\n\tborder-top-color: transparent;\n\tborder-right-color: transparent;\n\tborder-left-color: transparent;\n\tborder-bottom-color: transparent;\n\tborder-bottom-width: 1px;\n\tborder-style: solid;\n\tcolor: #808086;\n\tpadding: 3px;\n\tmargin-left:3px;\n\tbackground-color: #1e1d23;\n}\nQTabBar::tab:selected, QTabBar::tab:last:selected, QTabBar::tab:hover {\n \tborder-style: solid;\n\tborder-top-color: transparent;\n\tborder-right-color: transparent;\n\tborder-left-color: transparent;\n\tborder-bottom-color: #04b97f;\n\tborder-bottom-width: 2px;\n\tborder-style: solid;\n\tcolor: #FFFFFF;\n\tpadding-left: 3px;\n\tpadding-bottom: 2px;\n\tmargin-left:3px;\n\tbackground-color: #1e1d23;\n}\n\nQCheckBox {\n\tcolor: #a9b7c6;\n\tpadding: 2px;\n}\nQCheckBox:disabled {\n\tcolor: #808086;\n\tpadding: 2px;\n}\n\nQCheckBox:hover {\n\tborder-radius:4px;\n\tborder-style:solid;\n\tpadding-left: 1px;\n\tpadding-right: 1px;\n\tpadding-bottom: 1px;\n\tpadding-top: 1px;\n\tborder-width:1px;\n\tborder-color: rgb(87, 97, 106);\n\tbackground-color:#1e1d23;\n}\nQCheckBox::indicator:checked {\n\n\theight: 10px;\n\twidth: 10px;\n\tborder-style:solid;\n\tborder-width: 1px;\n\tborder-color: #04b97f;\n\tcolor: #a9b7c6;\n\tbackground-color: #04b97f;\n}\nQCheckBox::indicator:unchecked {\n\n\theight: 10px;\n\twidth: 10px;\n\tborder-style:solid;\n\tborder-width: 1px;\n\tborder-color: #04b97f;\n\tcolor: #a9b7c6;\n\tbackground-color: transparent;\n}\nQRadioButton {\n\tcolor: #a9b7c6;\n\tbackground-color: #1e1d23;\n\tpadding: 1px;\n}\nQRadioButton::indicator:checked {\n\theight: 10px;\n\twidth: 10px;\n\tborder-style:solid;\n\tborder-radius:5px;\n\tborder-width: 1px;\n\tborder-color: #04b97f;\n\tcolor: #a9b7c6;\n\tbackground-color: #04b97f;\n}\nQRadioButton::indicator:!checked {\n\theight: 10px;\n\twidth: 10px;\n\tborder-style:solid;\n\tborder-radius:5px;\n\tborder-width: 1px;\n\tborder-color: #04b97f;\n\tcolor: #a9b7c6;\n\tbackground-color: transparent;\n}\nQStatusBar {\n\tcolor:#027f7f;\n}\nQSpinBox {\n\tcolor: #a9b7c6;\t\n\tbackground-color: #1e1d23;\n}\nQDoubleSpinBox {\n\tcolor: #a9b7c6;\t\n\tbackground-color: #1e1d23;\n}\nQTimeEdit {\n\tcolor: #a9b7c6;\t\n\tbackground-color: #1e1d23;\n}\nQDateTimeEdit {\n\tcolor: #a9b7c6;\t\n\tbackground-color: #1e1d23;\n}\nQDateEdit {\n\tcolor: #a9b7c6;\t\n\tbackground-color: #1e1d23;\n}\nQComboBox {\n\tcolor: #a9b7c6;\t\n\tbackground: #1e1d23;\n}\nQComboBox:editable {\n\tbackground: #1e1d23;\n\tcolor: #a9b7c6;\n\tselection-background-color: #1e1d23;\n}\nQComboBox QAbstractItemView {\n\tcolor: #a9b7c6;\t\n\tbackground: #1e1d23;\n\tselection-color: #FFFFFF;\n\tselection-background-color: #1e1d23;\n}\nQComboBox:!editable:on, QComboBox::drop-down:editable:on {\n\tcolor: #a9b7c6;\t\n\tbackground: #1e1d23;\n}\nQFontComboBox {\n\tcolor: #a9b7c6;\t\n\tbackground-color: #1e1d23;\n}\nQToolBox {\n\tcolor: #a9b7c6;\n\tbackground-color: #1e1d23;\n}\nQToolBox::tab {\n\tcolor: #a9b7c6;\n\tbackground-color: #1e1d23;\n}\nQToolBox::tab:selected {\n\tcolor: #FFFFFF;\n\tbackground-color: #1e1d23;\n}\nQScrollArea {\n\tcolor: #FFFFFF;\n\tbackground-color: #1e1d23;\n}\nQSlider::groove:horizontal {\n\theight: 5px;\n\tbackground: #04b97f;\n}\nQSlider::groove:vertical {\n\twidth: 5px;\n\tbackground: #04b97f;\n}\nQSlider::handle:horizontal {\n\tbackground: qlineargradient(x1:0, y1:0, x2:1, y2:1, stop:0 #b4b4b4, stop:1 #8f8f8f);\n\tborder: 0px solid #5c5c5c;\n\twidth: 14px;\n\tmargin: -5px 0;\n\tborder-radius: 7px;\n}\nQSlider::handle:vertical {\n\tbackground: qlineargradient(x1:1, y1:1, x2:0, y2:0, stop:0 #b4b4b4, stop:1 #8f8f8f);\n\tborder: 0px solid #5c5c5c;\n\theight: 14px;\n\tmargin: 0 -5px;\n\tborder-radius: 7px;\n}\nQSlider::add-page:horizontal {\n background: white;\n}\nQSlider::add-page:vertical {\n background: white;\n}\nQSlider::sub-page:horizontal {\n background: #04b97f;\n}\nQSlider::sub-page:vertical {\n background: #04b97f;\n}\n'
|
16 |
+
|
17 |
+
class MainWindow(QtWidgets.QMainWindow):
|
18 |
+
def __init__(self):
|
19 |
+
super().__init__()
|
20 |
+
self.load_tree()
|
21 |
+
self.init_widgets()
|
22 |
+
self.init_tree()
|
23 |
+
|
24 |
+
def load_tree(self):
|
25 |
+
self.process_log = []
|
26 |
+
root = tkinter.Tk()
|
27 |
+
root.withdraw()
|
28 |
+
print('Please select the root directory of the V3Det dataset!')
|
29 |
+
self.root = filedialog.askdirectory()
|
30 |
+
self.tree = {}
|
31 |
+
self.visual_cat_list = []
|
32 |
+
try:
|
33 |
+
with open(os.path.join(self.root, 'annotations', 'v3det_2023_v1_category_tree.json'), encoding='utf-8') as f:
|
34 |
+
self.tree = json.load(f)
|
35 |
+
self.im_root = os.path.join(self.root, 'images')
|
36 |
+
self.visual_cat_list = os.listdir(self.im_root)
|
37 |
+
print('Images and category_tree.json are all loaded!')
|
38 |
+
except:
|
39 |
+
if len(self.tree) == 0:
|
40 |
+
print('Error: There is no tree.json!')
|
41 |
+
else:
|
42 |
+
if len(self.visual_cat_list) == 0:
|
43 |
+
print('Warning: There is no images!')
|
44 |
+
print('Please make sure the structure of files, and select the root directory:')
|
45 |
+
print('| -- V3Det(root)')
|
46 |
+
print(' | -- images')
|
47 |
+
print(' | -- n00001234')
|
48 |
+
print(' | -- ...jpg')
|
49 |
+
print(' | -- annotations')
|
50 |
+
print(' | -- v3det_2023_v1_category_tree.json')
|
51 |
+
if len(self.tree) == 0:
|
52 |
+
exit()
|
53 |
+
|
54 |
+
def init_widgets(self):
|
55 |
+
# main layout
|
56 |
+
central_widget = QtWidgets.QWidget()
|
57 |
+
main_layout = QtWidgets.QHBoxLayout()
|
58 |
+
central_widget.setLayout(main_layout)
|
59 |
+
|
60 |
+
left_layout = QtWidgets.QVBoxLayout()
|
61 |
+
search_box = QtWidgets.QHBoxLayout()
|
62 |
+
self.search_text = QLineEdit()
|
63 |
+
self.search_text.textEdited.connect(self.text_change)
|
64 |
+
self.search_button = QPushButton("Search")
|
65 |
+
self.search_button.pressed.connect(self.search)
|
66 |
+
search_box.addWidget(self.search_text)
|
67 |
+
search_box.addWidget(self.search_button)
|
68 |
+
left_layout.addLayout(search_box)
|
69 |
+
|
70 |
+
self.visual_tree = QTreeWidget()
|
71 |
+
self.visual_tree.setColumnCount(2)
|
72 |
+
self.visual_tree.setHeaderLabels(['name_en', 'name_zh'])
|
73 |
+
left_layout.addWidget(self.visual_tree)
|
74 |
+
|
75 |
+
node_lay = QtWidgets.QHBoxLayout()
|
76 |
+
self.current_node = QTreeWidget()
|
77 |
+
self.current_node.setColumnCount(2)
|
78 |
+
self.current_node.setHeaderLabels(['name_en', 'name_zh'])
|
79 |
+
node_lay.addWidget(self.current_node)
|
80 |
+
|
81 |
+
self.search_list = QTreeWidget()
|
82 |
+
self.search_list.setColumnCount(2)
|
83 |
+
self.search_list.setHeaderLabels(['name_en', 'name_zh/num'])
|
84 |
+
node_lay.addWidget(self.search_list)
|
85 |
+
left_layout.addLayout(node_lay)
|
86 |
+
|
87 |
+
main_layout.addLayout(left_layout)
|
88 |
+
|
89 |
+
right_layout = QtWidgets.QVBoxLayout()
|
90 |
+
info_layout = QtWidgets.QVBoxLayout()
|
91 |
+
self.class_id = QLabel("class_id")
|
92 |
+
self.class_name_en = QLabel("class_name_en")
|
93 |
+
self.class_desc_en = QLabel("class_desc_en")
|
94 |
+
self.class_name_zh = QLabel("class_name_zh")
|
95 |
+
self.class_desc_zh = QLabel("class_desc_zh")
|
96 |
+
self.class_id.setTextInteractionFlags(Qt.TextSelectableByMouse)
|
97 |
+
self.class_name_en.setTextInteractionFlags(Qt.TextSelectableByMouse)
|
98 |
+
self.class_name_en.setWordWrap(True)
|
99 |
+
self.class_desc_en.setTextInteractionFlags(Qt.TextSelectableByMouse)
|
100 |
+
self.class_desc_en.setWordWrap(True)
|
101 |
+
self.class_name_zh.setTextInteractionFlags(Qt.TextSelectableByMouse)
|
102 |
+
self.class_name_zh.setWordWrap(True)
|
103 |
+
self.class_desc_zh.setTextInteractionFlags(Qt.TextSelectableByMouse)
|
104 |
+
self.class_desc_zh.setWordWrap(True)
|
105 |
+
self.class_name_en.setFixedWidth(W)
|
106 |
+
self.class_desc_en.setFixedWidth(W)
|
107 |
+
self.class_name_zh.setFixedWidth(W)
|
108 |
+
self.class_desc_zh.setFixedWidth(W)
|
109 |
+
info_layout.addWidget(self.class_id, QtCore.Qt.AlignLeft)
|
110 |
+
info_layout.addWidget(self.class_name_en, QtCore.Qt.AlignLeft)
|
111 |
+
info_layout.addWidget(self.class_name_zh, QtCore.Qt.AlignLeft)
|
112 |
+
info_layout.addWidget(self.class_desc_en, QtCore.Qt.AlignLeft)
|
113 |
+
info_layout.addWidget(self.class_desc_zh, QtCore.Qt.AlignLeft)
|
114 |
+
right_layout.addLayout(info_layout)
|
115 |
+
|
116 |
+
self.class_images = []
|
117 |
+
num_items = 4
|
118 |
+
for num_im_i in range(num_items):
|
119 |
+
img_layout = QtWidgets.QHBoxLayout()
|
120 |
+
for num_im_j in range(num_items):
|
121 |
+
image = CanvasImage(1 / num_items)
|
122 |
+
self.class_images.append(image)
|
123 |
+
img_layout.addWidget(image)
|
124 |
+
right_layout.addLayout(img_layout)
|
125 |
+
|
126 |
+
main_layout.addLayout(right_layout)
|
127 |
+
|
128 |
+
self.setCentralWidget(central_widget)
|
129 |
+
self.setWindowIcon(QIcon('icon.png'))
|
130 |
+
self.setStyleSheet(qss)
|
131 |
+
|
132 |
+
self.visual_tree.itemClicked.connect(
|
133 |
+
functools.partial(self.onitemSelectionChanged, self.visual_tree))
|
134 |
+
self.current_node.itemClicked.connect(
|
135 |
+
functools.partial(self.onitemSelectionChanged, self.current_node))
|
136 |
+
self.search_list.itemClicked.connect(
|
137 |
+
functools.partial(self.onitemSelectionChanged, self.search_list))
|
138 |
+
|
139 |
+
self.visual_tree.itemSelectionChanged.connect(
|
140 |
+
functools.partial(self.onitemSelectionChanged, self.visual_tree))
|
141 |
+
self.current_node.itemSelectionChanged.connect(
|
142 |
+
functools.partial(self.onitemSelectionChanged, self.current_node))
|
143 |
+
self.search_list.itemSelectionChanged.connect(
|
144 |
+
functools.partial(self.onitemSelectionChanged, self.search_list))
|
145 |
+
|
146 |
+
self.visual_tree.itemDoubleClicked.connect(
|
147 |
+
functools.partial(self.onitemSelectionChanged, self.visual_tree))
|
148 |
+
self.current_node.itemDoubleClicked.connect(
|
149 |
+
functools.partial(self.onitemSelectionChanged, self.current_node))
|
150 |
+
self.search_list.itemDoubleClicked.connect(
|
151 |
+
functools.partial(self.onitemSelectionChanged, self.search_list))
|
152 |
+
|
153 |
+
def init_tree(self):
|
154 |
+
child_num = {}
|
155 |
+
|
156 |
+
def count_child(node):
|
157 |
+
num = 1
|
158 |
+
if node in self.tree['father2child']:
|
159 |
+
for child in self.tree['father2child'][node]:
|
160 |
+
num += count_child(child)
|
161 |
+
child_num[node] = num
|
162 |
+
return num
|
163 |
+
count_child('n00001740')
|
164 |
+
self.child_num = child_num
|
165 |
+
|
166 |
+
self.show_tree()
|
167 |
+
|
168 |
+
def show_tree(self, node='n00001740', level=0, parent=None):
|
169 |
+
name_en = self.tree['id2name'][node]
|
170 |
+
name_en = name_en + '({})'.format(self.child_num[node])
|
171 |
+
name_zh = str(self.tree['id2name_zh'][node]) if node in self.tree['id2name_zh'] else ''
|
172 |
+
current = QtWidgets.QTreeWidgetItem(["{}-{}".format(level, name_en), name_zh])
|
173 |
+
|
174 |
+
current.id = node
|
175 |
+
if node == 'n00001740':
|
176 |
+
self.visual_tree.addTopLevelItem(current)
|
177 |
+
else:
|
178 |
+
parent.addChild(current)
|
179 |
+
if node in self.tree['father2child']:
|
180 |
+
for child in self.tree['father2child'][node]:
|
181 |
+
self.show_tree(child, level + 1, current)
|
182 |
+
|
183 |
+
def text_change(self):
|
184 |
+
if self.search_button.text() == 'Reset':
|
185 |
+
self.search(False)
|
186 |
+
if len(self.search_text.text().encode('utf-8')) > 1:
|
187 |
+
self.search(False)
|
188 |
+
|
189 |
+
def search(self, text_reset=True):
|
190 |
+
if self.search_button.text() == 'Search':
|
191 |
+
text = self.search_text.text()
|
192 |
+
if len(text) != 0:
|
193 |
+
searched_list = [class_id for class_id in self.tree['id2name'].keys() if class_id == text]
|
194 |
+
for item in ['id2name', 'id2name_zh', 'id2desc', 'id2desc_zh']:
|
195 |
+
for k, v in self.tree[item].items():
|
196 |
+
if k not in self.tree.get('child2father'):
|
197 |
+
continue
|
198 |
+
if isinstance(v, list):
|
199 |
+
names = ''
|
200 |
+
for vi in v:
|
201 |
+
names += vi
|
202 |
+
v = names
|
203 |
+
if isinstance(v, str):
|
204 |
+
if (k not in searched_list) and (text.lower() in v.lower()):
|
205 |
+
if k in self.tree['id2name']:
|
206 |
+
searched_list.append(k)
|
207 |
+
|
208 |
+
self.insert_num = len(searched_list)
|
209 |
+
self.show_list(searched_list)
|
210 |
+
|
211 |
+
self.search_button.setText('Reset')
|
212 |
+
|
213 |
+
elif self.search_button.text() == 'Reset':
|
214 |
+
self.search_list.clear()
|
215 |
+
self.insert_num = 0
|
216 |
+
if text_reset:
|
217 |
+
self.search_text.setText('')
|
218 |
+
self.search_button.setText('Search')
|
219 |
+
|
220 |
+
def show_list(self, show_list, insert_index=-1):
|
221 |
+
num_id = 0
|
222 |
+
for cat_id in show_list:
|
223 |
+
if len(cat_id) == 0:
|
224 |
+
continue
|
225 |
+
num_id += 1
|
226 |
+
name_en = self.tree['id2name'][cat_id] if len(self.tree['id2name'][cat_id]) else ''
|
227 |
+
name_en = name_en + '({})'.format(len(self.tree['ancestor2descendant'].get(cat_id, set())))
|
228 |
+
name_zh = self.tree['id2name_zh'].get(cat_id, '')
|
229 |
+
cur = QtWidgets.QTreeWidgetItem(["{}-{}".format(num_id, name_en), name_zh])
|
230 |
+
cur.id = cat_id
|
231 |
+
|
232 |
+
insert_id = insert_index if insert_index >= 0 else self.search_list.topLevelItemCount()
|
233 |
+
self.search_list.insertTopLevelItem(insert_id, cur)
|
234 |
+
|
235 |
+
def onItemClicked(self, class_id):
|
236 |
+
self.class_id.setText(class_id)
|
237 |
+
self.class_name_en.setText(self.tree['id2name'][class_id])
|
238 |
+
self.class_desc_en.setText(self.tree['id2desc'].get(class_id, ''))
|
239 |
+
self.class_name_zh.setText(self.tree['id2name_zh'].get(class_id, ''))
|
240 |
+
self.class_desc_zh.setText(self.tree['id2desc_zh'].get(class_id, ''))
|
241 |
+
|
242 |
+
[im.clean() for im in self.class_images]
|
243 |
+
if class_id in self.visual_cat_list:
|
244 |
+
show_num = len(self.class_images)
|
245 |
+
folder = os.path.join(self.im_root, class_id)
|
246 |
+
im_names = os.listdir(folder)
|
247 |
+
num_image = 0
|
248 |
+
for im_i in im_names:
|
249 |
+
if num_image == show_num:
|
250 |
+
break
|
251 |
+
self.class_images[num_image].load_image(os.path.join(folder, im_i))
|
252 |
+
num_image += 1
|
253 |
+
|
254 |
+
self.add_current(class_id)
|
255 |
+
|
256 |
+
def add_current(self, class_id):
|
257 |
+
name_en = self.tree['id2name'][class_id]
|
258 |
+
name_en = name_en + '({})'.format(len(self.tree['ancestor2descendant'].get(class_id, set())))
|
259 |
+
name_zh = self.tree['id2name_zh'].get(class_id, '')
|
260 |
+
self.current_node.clear()
|
261 |
+
current = QtWidgets.QTreeWidgetItem([name_en, name_zh])
|
262 |
+
current.id = class_id
|
263 |
+
|
264 |
+
self.current_node.addTopLevelItem(current)
|
265 |
+
for relation in ['father2child', 'child2father', 'id2synonym_list', 'ancestor2descendant', 'descendant2ancestor']:
|
266 |
+
rel = QtWidgets.QTreeWidgetItem([relation, str(len(self.tree[relation].get(class_id, [])))])
|
267 |
+
current.addChild(rel)
|
268 |
+
for rel_class in self.tree[relation].get(class_id, []):
|
269 |
+
name_en = self.tree['id2name'][rel_class]
|
270 |
+
name_en = name_en + '({})'.format(
|
271 |
+
len(self.tree['ancestor2descendant'].get(rel_class, set())))
|
272 |
+
name_zh = self.tree['id2name_zh'].get(rel_class, '')
|
273 |
+
rel_node = QtWidgets.QTreeWidgetItem([name_en, name_zh])
|
274 |
+
rel_node.id = rel_class
|
275 |
+
rel.addChild(rel_node)
|
276 |
+
self.current_node.expandAll()
|
277 |
+
|
278 |
+
def onitemSelectionChanged(self, node_tree):
|
279 |
+
if node_tree.selectedItems():
|
280 |
+
if hasattr(node_tree.selectedItems()[0], 'id'):
|
281 |
+
self.onItemClicked(node_tree.selectedItems()[0].id)
|
282 |
+
|
283 |
+
def keyPressEvent(self, qKeyEvent):
|
284 |
+
super().keyPressEvent(qKeyEvent)
|
285 |
+
|
286 |
+
|
287 |
+
class CanvasImage(QtWidgets.QLabel):
|
288 |
+
def __init__(self, scale=1.):
|
289 |
+
super().__init__()
|
290 |
+
self.save_root = 'select'
|
291 |
+
self.scale = scale
|
292 |
+
self.clean()
|
293 |
+
|
294 |
+
def load_image(self, path):
|
295 |
+
pixmap = QPixmap(path)
|
296 |
+
w, h = pixmap.width(), pixmap.height()
|
297 |
+
r = min(self.scale * W / w, self.scale * H / h)
|
298 |
+
w, h = int(r * w), int(r * h)
|
299 |
+
pixmap = pixmap.scaled(w, h, Qt.IgnoreAspectRatio, Qt.SmoothTransformation)
|
300 |
+
|
301 |
+
self.setAlignment(Qt.AlignCenter | Qt.AlignCenter)
|
302 |
+
self.setPixmap(pixmap)
|
303 |
+
|
304 |
+
def clean(self):
|
305 |
+
pixmap = QtGui.QPixmap(int(W * self.scale), int(H * self.scale))
|
306 |
+
pixmap.fill(QColor("white"))
|
307 |
+
self.setPixmap(pixmap)
|
308 |
+
|
309 |
+
|
310 |
+
if __name__ == '__main__':
|
311 |
+
app = QtWidgets.QApplication(sys.argv)
|
312 |
+
app.setStyle('Material')
|
313 |
+
window = MainWindow()
|
314 |
+
window.show()
|
315 |
+
app.exec_()
|
316 |
+
|