質問編集履歴
3
前回との大幅な質問内容の変更
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,17 +1,10 @@
|
|
1
1
|
Pythonを始めたばかりです。
|
2
2
|
|
3
|
-
大学の課題でYolov5の実装を行い、dataディレクトリに格納した画像の物体検知を行いました。
|
4
|
-
|
3
|
+
大学の課題でアノテーションデータと画像データを読み込み、Yoloの形式に変換するプログラムを作成する課題を行います。
|
5
4
|
|
6
|
-
|
5
|
+
参考プログラムをいただいたのですが、アノテーションデータと画像データの読み込み・バウンティボックスを使った検出する物体の中心座標等の取得など大まかな構成は理解したのですが、詳しい流れが上手く理解できません。
|
7
|
-
どなたか教えていただけませんか?
|
8
6
|
|
9
|
-
課題の流れ
|
10
|
-
|
7
|
+
どなたか教えていただけないでしょうか?
|
11
|
-
・対応画像の読み込み
|
12
|
-
・対応画像のメタデータ(画像全体の縦横サイズ)の取得
|
13
|
-
・アノテーションデータを,割合値からpx値へ変換
|
14
|
-
・変換したデータの書き込み
|
15
8
|
|
16
9
|
参考プログラムです。
|
17
10
|
```python
|
2
分かりやすくした
test
CHANGED
File without changes
|
test
CHANGED
@@ -6,58 +6,68 @@
|
|
6
6
|
ZIPファイルで画像と変換前のアノテーションデータをダウンロードしたのですが、そこから変換の手法がよく分かりません。
|
7
7
|
どなたか教えていただけませんか?
|
8
8
|
|
9
|
+
課題の流れ
|
10
|
+
・アノテーションデータの読み込み
|
11
|
+
・対応画像の読み込み
|
12
|
+
・対応画像のメタデータ(画像全体の縦横サイズ)の取得
|
13
|
+
・アノテーションデータを,割合値からpx値へ変換
|
14
|
+
・変換したデータの書き込み
|
15
|
+
|
9
16
|
参考プログラムです。
|
17
|
+
```python
|
10
18
|
|
11
19
|
import glob
|
12
|
-
import os
|
20
|
+
import os #os.path.split
|
13
21
|
import cv2 as cv
|
14
22
|
#import csv #output
|
15
23
|
|
16
24
|
translated = ['0' for i in range(5)]
|
17
|
-
annos = glob.glob("./imput/labels_valid/
|
25
|
+
annos = glob.glob("./imput/labels_valid/.txt") #INPUT DIR
|
18
|
-
imgs = glob.glob("./imput/imgs_valid/
|
26
|
+
imgs = glob.glob("./imput/imgs_valid/.jpg") #get img size
|
19
27
|
|
20
28
|
data = [' 'for i in range(5)]
|
21
29
|
|
22
|
-
dir_path = './output/labels/'
|
30
|
+
dir_path = './output/labels/' #OUTPUT DIR
|
23
31
|
os.makedirs(dir_path, exist_ok = True)
|
24
32
|
|
25
|
-
for i, i_path in enumerate(annos):
|
33
|
+
for i, i_path in enumerate(annos):
|
26
|
-
|
34
|
+
f = open(i_path) #existing
|
27
|
-
|
35
|
+
nf = open(dir_path + os.path.split(i_path)[1] , 'w', newline = '') #create file
|
28
|
-
|
36
|
+
#writer = csv.writer(nf)
|
29
37
|
|
30
|
-
|
38
|
+
im = cv.imread(imgs[i])
|
31
|
-
|
39
|
+
h,w,_ = im.shape #h : img height , w : width , _ : color map (dont use)
|
32
40
|
|
33
|
-
|
41
|
+
for j,line in enumerate(f):
|
34
|
-
|
42
|
+
data[0] = (int)((line.split(',')[5])) #category
|
35
|
-
|
43
|
+
data[1] = (int)((line.split(',')[0])) #bbox left-top x
|
36
|
-
|
44
|
+
data[2] = (int)((line.split(',')[1])) #bbox left-top y
|
37
|
-
|
45
|
+
data[3] = (int)((line.split(',')[2])) #bbox width
|
38
|
-
|
46
|
+
data[4] = (int)((line.split(',')[3])) #bbox height
|
39
47
|
|
40
|
-
|
48
|
+
#===ABOUT category===
|
41
|
-
|
49
|
+
#regions (0), pedestrian (1), people (2), bicycle (3), car (4), van (5),
|
42
|
-
|
50
|
+
#truck (6), tricycle (7), awning-tricycle (8), bus (9), motor (10), others (11)
|
43
51
|
|
44
|
-
|
52
|
+
data[3] = (data[3] / w) #width (ratio)
|
45
|
-
|
53
|
+
data[4] = (data[4] / h) #h
|
46
54
|
|
47
|
-
|
55
|
+
data[1] = (data[1] / w) + (data[3])/2 #bbox center x
|
48
|
-
|
56
|
+
data[2] = (data[2] / h) + (data[4])/2 #bbox center y
|
49
57
|
|
50
|
-
|
58
|
+
data[3] = '{:.6g}'.format(data[3]) #width (ratio)
|
51
|
-
|
59
|
+
data[4] = '{:.6g}'.format(data[4]) #h
|
52
60
|
|
53
|
-
|
61
|
+
data[1] = '{:.6g}'.format(data[1]) #bbox center x
|
54
|
-
|
62
|
+
data[2] = '{:.6g}'.format(data[2]) #bbox center y
|
55
63
|
|
56
|
-
|
64
|
+
#out_list = (str)(data[0])+' '+(str)(data[1])+' '+(str)(data[2])+' '+(str)(data[3])+' '+(str)(data[4])
|
57
|
-
|
65
|
+
#csv.writer(nf)
|
58
66
|
|
59
|
-
|
67
|
+
if data[0] >= 1 and data[0] <= 10: #delete regions & others
|
60
|
-
|
68
|
+
data[0] -= 1
|
61
|
-
|
69
|
+
nf.writelines((str)(data[0])+' '+(str)(data[1])+' '+(str)(data[2])+' '+(str)(data[3])+' '+(str)(data[4])+'\n')
|
62
|
-
|
70
|
+
#writer.writerow(out_list)
|
63
|
-
|
71
|
+
```
|
72
|
+
|
73
|
+
|
1
参考プログラム追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -5,3 +5,59 @@
|
|
5
5
|
|
6
6
|
ZIPファイルで画像と変換前のアノテーションデータをダウンロードしたのですが、そこから変換の手法がよく分かりません。
|
7
7
|
どなたか教えていただけませんか?
|
8
|
+
|
9
|
+
参考プログラムです。
|
10
|
+
|
11
|
+
import glob
|
12
|
+
import os #os.path.split
|
13
|
+
import cv2 as cv
|
14
|
+
#import csv #output
|
15
|
+
|
16
|
+
translated = ['0' for i in range(5)]
|
17
|
+
annos = glob.glob("./imput/labels_valid/*.txt") #INPUT DIR
|
18
|
+
imgs = glob.glob("./imput/imgs_valid/*.jpg") #get img size
|
19
|
+
|
20
|
+
data = [' 'for i in range(5)]
|
21
|
+
|
22
|
+
dir_path = './output/labels/' #OUTPUT DIR
|
23
|
+
os.makedirs(dir_path, exist_ok = True)
|
24
|
+
|
25
|
+
for i, i_path in enumerate(annos):
|
26
|
+
f = open(i_path) #existing
|
27
|
+
nf = open(dir_path + os.path.split(i_path)[1] , 'w', newline = '') #create file
|
28
|
+
#writer = csv.writer(nf)
|
29
|
+
|
30
|
+
im = cv.imread(imgs[i])
|
31
|
+
h,w,_ = im.shape #h : img height , w : width , _ : color map (dont use)
|
32
|
+
|
33
|
+
for j,line in enumerate(f):
|
34
|
+
data[0] = (int)((line.split(',')[5])) #category
|
35
|
+
data[1] = (int)((line.split(',')[0])) #bbox left-top x
|
36
|
+
data[2] = (int)((line.split(',')[1])) #bbox left-top y
|
37
|
+
data[3] = (int)((line.split(',')[2])) #bbox width
|
38
|
+
data[4] = (int)((line.split(',')[3])) #bbox height
|
39
|
+
|
40
|
+
#===ABOUT category===
|
41
|
+
#regions (0), pedestrian (1), people (2), bicycle (3), car (4), van (5),
|
42
|
+
#truck (6), tricycle (7), awning-tricycle (8), bus (9), motor (10), others (11)
|
43
|
+
|
44
|
+
data[3] = (data[3] / w) #width (ratio)
|
45
|
+
data[4] = (data[4] / h) #h
|
46
|
+
|
47
|
+
data[1] = (data[1] / w) + (data[3])/2 #bbox center x
|
48
|
+
data[2] = (data[2] / h) + (data[4])/2 #bbox center y
|
49
|
+
|
50
|
+
data[3] = '{:.6g}'.format(data[3]) #width (ratio)
|
51
|
+
data[4] = '{:.6g}'.format(data[4]) #h
|
52
|
+
|
53
|
+
data[1] = '{:.6g}'.format(data[1]) #bbox center x
|
54
|
+
data[2] = '{:.6g}'.format(data[2]) #bbox center y
|
55
|
+
|
56
|
+
#out_list = (str)(data[0])+' '+(str)(data[1])+' '+(str)(data[2])+' '+(str)(data[3])+' '+(str)(data[4])
|
57
|
+
#csv.writer(nf)
|
58
|
+
|
59
|
+
if data[0] >= 1 and data[0] <= 10: #delete regions & others
|
60
|
+
data[0] -= 1
|
61
|
+
nf.writelines((str)(data[0])+' '+(str)(data[1])+' '+(str)(data[2])+' '+(str)(data[3])+' '+(str)(data[4])+'\n')
|
62
|
+
#writer.writerow(out_list)
|
63
|
+
|