-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit a86f11c
Showing
41 changed files
with
4,012 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
import json | ||
import os | ||
import cv2 | ||
import numpy as np | ||
from sklearn.metrics import confusion_matrix | ||
import csv | ||
|
||
import torch | ||
|
||
def drawCounters(counters, img, color=(0, 0, 255)): | ||
for counter in counters: | ||
cv2.rectangle(img, (counter[1], counter[0]), (counter[3], counter[2]), color, 4) | ||
return img | ||
def drawFillCounters(counters, img): | ||
for counter in counters: | ||
cv2.rectangle(img, (counter[1], counter[0]), (counter[3], counter[2]), (255, 255, 255), -1) | ||
return img | ||
def yoloBoxToCv2(detection, shape): | ||
height = shape[0] | ||
width = shape[1] | ||
center_x = int(detection[1] * width) | ||
center_y = int(detection[2] * height) | ||
w = int(detection[3] * width) | ||
h = int(detection[4] * height) | ||
x = int(center_x - w / 2) | ||
y = int(center_y - h / 2) | ||
return [y, x, y+h, x+w] | ||
def countIOU(rec1, rec2): | ||
S_rec1 = (rec1[2] - rec1[0]) * (rec1[3] - rec1[1]) | ||
S_rec2 = (rec2[2] - rec2[0]) * (rec2[3] - rec2[1]) | ||
sum_area = S_rec1 + S_rec2 | ||
|
||
left_line = max(rec1[1], rec2[1]) | ||
right_line = min(rec1[3], rec2[3]) | ||
top_line = max(rec1[0], rec2[0]) | ||
bottom_line = min(rec1[2], rec2[2]) | ||
if left_line >= right_line or top_line >= bottom_line: | ||
return 0, -1, S_rec1, S_rec2 | ||
else: | ||
intersect = (right_line - left_line) * (bottom_line - top_line) | ||
return (intersect / (sum_area - intersect))*1.0, intersect, S_rec1, S_rec2 | ||
def testMask(preImage, gtImage): | ||
preImage = preImage[:,:,0].reshape((-1))//255 | ||
gtImage = gtImage[:,:,0].reshape((-1))//255 | ||
|
||
preImage_torch = torch.from_numpy(preImage).cuda() | ||
gtImage_torch = torch.from_numpy(gtImage).cuda() | ||
|
||
TP = torch.sum((gtImage_torch == 1) & (preImage_torch == 1)).item() #gpu version | ||
FN = torch.sum((gtImage_torch == 0) & (preImage_torch == 1)).item() | ||
FP = torch.sum((gtImage_torch == 1) & (preImage_torch == 0)).item() | ||
TN = torch.sum((gtImage_torch == 0) & (preImage_torch == 0)).item() | ||
|
||
# accuracy = confusion_matrix(preImage, gtImage, labels=[1, 0]) #cpu version | ||
# TP = accuracy[0,0] | ||
# FN = accuracy[0,1] | ||
# FP = accuracy[1,0] | ||
# TN = accuracy[1,1] | ||
# print(accuracy) | ||
P = TP/(TP+FP) | ||
R = TP/(TP+FN) | ||
F1 = 2*P*R/(P+R) | ||
|
||
Acc = (TP+TN)/(TP+TN+FP+FN) | ||
print("Accuracy =", Acc) | ||
print("Precision=", P) | ||
print("Recall =", R) | ||
print("F1 =", F1) | ||
|
||
Acc = "{:.4f}".format(Acc*100) | ||
P = "{:.4f}".format(P*100) | ||
R = "{:.4f}".format(R*100) | ||
F1 = "{:.4f}".format(F1*100) | ||
writer.writerow([Acc, P, R, F1]) | ||
|
||
UnetImgFlooder = "./result/marge/Unet/" | ||
GTFlooders = "./groundtruth/marge" | ||
storeFlooder = "./storeImg/" | ||
|
||
|
||
with open('result.csv', 'w', newline='') as csvfile: | ||
writer = csv.writer(csvfile) | ||
|
||
for GTFlooderName in os.listdir(GTFlooders): | ||
print(GTFlooderName) | ||
GTPath = os.path.join(GTFlooders, GTFlooderName) | ||
|
||
ImgPath = os.path.join(GTPath, GTFlooderName + ".jpg") | ||
GTTxtPath = os.path.join(GTPath, GTFlooderName + ".txt") | ||
classNamePath = os.path.join(GTPath, "classes.txt") | ||
|
||
UnetImgPath = os.path.join(UnetImgFlooder, GTFlooderName + ".png") | ||
|
||
print(ImgPath) | ||
image = cv2.imread(ImgPath) | ||
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) | ||
image = cv2.cvtColor(image, cv2.COLOR_GRAY2BGR) | ||
|
||
imagePreMask = cv2.imread(UnetImgPath)#讀取預測結果 | ||
# print(imagePreMask) | ||
|
||
with open(GTTxtPath,'r') as f: #讀取真實結果 | ||
fStrs = f.readlines() | ||
fStrs = [fStr.replace("\n", "") for fStr in fStrs] | ||
yoloRects = [[float(i) for i in fStr.split(" ")] for fStr in fStrs] | ||
cv2Rects = [yoloBoxToCv2(i, image.shape) for i in yoloRects] | ||
# image = drawCounters(cv2Rects, image, color=(0, 0, 255)) | ||
|
||
imageMaskGt = np.zeros(image.shape) | ||
imageMaskGt = drawFillCounters(cv2Rects, imageMaskGt) | ||
testMask(imagePreMask, imageMaskGt) | ||
|
||
# image = drawCounters(pairRectsA, image, color=(0, 255, 0)) | ||
cv2.imwrite(os.path.join(storeFlooder, GTFlooderName + ".jpg"), image) |
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,230 @@ | ||
import numpy as np | ||
import cv2 | ||
from PIL import ImageFont, ImageDraw, Image | ||
import random | ||
import os | ||
import json | ||
strLib = ['0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','_','(',')','/','#'] | ||
|
||
def drawGrid(draw, imgSize, gridSize): | ||
#style | ||
#0 空的 | ||
#1 邊框 | ||
#2 邊框有缺 | ||
#3 中心圓圈 | ||
#4 中心圓圈小的 | ||
#5 上方圓圈小的 | ||
#6 下方圓圈小的 | ||
colorful = random.randint(0, 1) | ||
style = random.randint(0, 6) | ||
|
||
for y in range((imgSize[0] // gridSize) + 1): | ||
for x in range((imgSize[1] // gridSize) + 1): | ||
|
||
if(colorful): | ||
gridBackgroundColor = (random.randint(0,255),random.randint(0,255),random.randint(0,255),255) #隨機顏色 | ||
draw.rectangle([x*gridSize, y*gridSize, (x+1)*gridSize, (y+1)*gridSize], fill = gridBackgroundColor) | ||
if(style == 1): | ||
draw.rectangle([x*gridSize, y*gridSize, (x+1)*gridSize, (y+1)*gridSize], fill = None, outline=0, width=2) | ||
if(style == 2): | ||
if(random.randint(0, 1)): | ||
draw.rectangle([x*gridSize, y*gridSize, (x+1)*gridSize, (y+1)*gridSize], fill = None, outline=0, width=2) | ||
if(style == 3): | ||
if(random.randint(0, 1)): #是否填色 | ||
draw.ellipse([(x*gridSize, y*gridSize), (x*gridSize+gridSize, y*gridSize+gridSize)] , | ||
fill = (random.randint(0,255),random.randint(0,255),random.randint(0,255)), | ||
outline ="black", width=2) | ||
else: | ||
draw.ellipse([(x*gridSize, y*gridSize), (x*gridSize+gridSize, y*gridSize+gridSize)] , | ||
fill = (random.randint(0,255),random.randint(0,255),random.randint(0,255)), | ||
outline =None, width=2) | ||
if(style == 4): | ||
if(random.randint(0, 1)): #是否填色 | ||
draw.ellipse([(x*gridSize+gridSize*0.25, y*gridSize+gridSize*0.25), (x*gridSize+gridSize*0.75, y*gridSize+gridSize*0.75)] , | ||
fill = (random.randint(0,255),random.randint(0,255),random.randint(0,255)), | ||
outline ="black", width=2) | ||
else: | ||
draw.ellipse([(x*gridSize+gridSize*0.25, y*gridSize+gridSize*0.25), (x*gridSize+gridSize*0.75, y*gridSize+gridSize*0.75)] , | ||
fill = (random.randint(0,255),random.randint(0,255),random.randint(0,255)), | ||
outline =None, width=2) | ||
if(style == 5): | ||
if(random.randint(0, 1)): #是否填色 | ||
draw.ellipse([(x*gridSize+gridSize*0.25, y*gridSize+gridSize*0.5), (x*gridSize+gridSize*0.75, y*gridSize+gridSize*1)] , | ||
fill = (random.randint(0,255),random.randint(0,255),random.randint(0,255)), | ||
outline ="black", width=2) | ||
else: | ||
draw.ellipse([(x*gridSize+gridSize*0.25, y*gridSize+gridSize*0.5), (x*gridSize+gridSize*0.75, y*gridSize+gridSize*1)] , | ||
fill = (random.randint(0,255),random.randint(0,255),random.randint(0,255)), | ||
outline =None, width=2) | ||
if(style == 6): | ||
if(random.randint(0, 1)): #是否填色 | ||
draw.ellipse([(x*gridSize+gridSize*0.25, y*gridSize), (x*gridSize+gridSize*0.75, y*gridSize+gridSize*0.5)] , | ||
fill = (random.randint(0,255),random.randint(0,255),random.randint(0,255)), | ||
outline ="black", width=2) | ||
else: | ||
draw.ellipse([(x*gridSize+gridSize*0.25, y*gridSize), (x*gridSize+gridSize*0.75, y*gridSize+gridSize*0.5)] , | ||
fill = (random.randint(0,255),random.randint(0,255),random.randint(0,255)), | ||
outline =None, width=2) | ||
|
||
|
||
return style | ||
def drawText(draw, font, imgSize, gridSize, maxStrLen=13, textOrientation=random.randint(0, 2)): | ||
#字方位編碼 | ||
#0:靠左 1:靠中 2:靠右 | ||
#textOrientation = (random.randint(0, 3), random.randint(0, 3)) | ||
textOrientation = 1 | ||
target = np.zeros((imgSize[0], imgSize[1], 3), np.uint8) | ||
for y in range((imgSize[0] // gridSize)): | ||
for x in range((imgSize[1] // gridSize)): | ||
hasNoStr = random.randint(0, 10) > 8 | ||
if(hasNoStr): | ||
continue | ||
textColor = (random.randint(0,255),random.randint(0,255),random.randint(0,255),255) #隨機的字顏色 | ||
strlen = random.randint(1, maxStrLen) | ||
drawUnderLine = random.randint(0, 10) > 8 | ||
drawTopLine = random.randint(0, 10) > 8 | ||
text = ''.join([strLib[x] for x in np.random.randint(len(strLib), size=strlen)]) | ||
baseY = y * gridSize + 3 | ||
baseX = x * gridSize + 3 | ||
endY = (y + 1) * gridSize - 3 | ||
endX = (x + 1) * gridSize - 3 | ||
|
||
textwidth, textheight = draw.textsize(text, font=font) | ||
if textOrientation == 0: | ||
nextY = baseY | ||
nextX = baseX | ||
elif textOrientation == 1: | ||
nextY = (baseY + endY) // 2 - textheight//2 | ||
nextX = baseX | ||
elif textOrientation == 2: | ||
nextY = endY - textheight*2 | ||
nextX = baseX | ||
|
||
|
||
|
||
|
||
for char in text: | ||
twidth, theight = draw.textsize(char, font=font) | ||
flag = False | ||
if char == '@': | ||
flag = True | ||
char = strLib[random.randint(0, 3)] | ||
nextY += theight//3 | ||
|
||
if(nextX + twidth > endX): | ||
nextX = baseX | ||
nextY += textheight | ||
if(nextY + textheight >= endY): | ||
break | ||
draw.text((nextX, nextY), char, font=font, fill=textColor) | ||
if (drawUnderLine): | ||
if flag: | ||
draw.line((nextX, nextY + textheight - theight/3, nextX + twidth, nextY + textheight - theight/3), fill=textColor) | ||
else: | ||
draw.line((nextX, nextY + textheight, nextX + twidth, nextY + textheight), fill=textColor) | ||
if (drawTopLine): | ||
if flag: | ||
draw.line((nextX, nextY - theight/3, nextX + twidth, nextY - theight/3), fill=textColor) | ||
else: | ||
draw.line((nextX, nextY, nextX + twidth, nextY), fill=textColor) | ||
|
||
ttwidth, ttheight = draw.textsize(char, font=font) | ||
|
||
#print(nextY, nextX, textheight, ttwidth) | ||
# if ((not drawTopLine) & (not drawUnderLine)): | ||
# target[nextY : nextY + textheight, nextX : nextX + ttwidth,2] = 1 | ||
# if (drawUnderLine & (not drawTopLine)): | ||
# target[nextY : nextY + textheight, nextX : nextX + ttwidth,2] = 2 | ||
# if (drawTopLine & (not drawUnderLine)): | ||
# target[nextY : nextY + textheight, nextX : nextX + ttwidth,2] = 3 | ||
# if (drawTopLine & drawUnderLine): | ||
# target[nextY : nextY + textheight, nextX : nextX + ttwidth,2] = 4 | ||
|
||
target[nextY : nextY + textheight, nextX : nextX + ttwidth, :] = 255 | ||
|
||
if flag: | ||
nextY -= theight//3 | ||
nextX += ttwidth | ||
else: | ||
nextX += twidth | ||
return target | ||
def drawLine(draw, imgSize, maxLineCount=50): | ||
for i in range(random.randint(1, maxLineCount)): | ||
x1 = random.randint(0, imgSize[1]) | ||
x2 = random.randint(0, imgSize[1]) | ||
y1 = random.randint(0, imgSize[0]) | ||
y2 = random.randint(0, imgSize[0]) | ||
color = (random.randint(0,255),random.randint(0,255),random.randint(0,255),255) #隨機的線顏色 | ||
draw.line((x1, y1, x2, y2), fill=color, width=random.randint(1,5)) | ||
def drawCircle(draw, imgSize, maxCircleCount=5): | ||
for i in range(random.randint(1, maxCircleCount)): | ||
x1 = random.randint(0, imgSize[1]) | ||
x2 = random.randint(0, imgSize[1]) | ||
y1 = random.randint(0, imgSize[0]) | ||
y2 = random.randint(0, imgSize[0]) | ||
color = (random.randint(0,255),random.randint(0,255),random.randint(0,255)) #隨機的線顏色 | ||
draw.ellipse((x1, y1, x2, y2), fill=None, outline=color, width=random.randint(1,5)) | ||
def createImg(): | ||
imgSizeHeight = random.randint(500, 1000) #隨機的圖片高度 | ||
imgSizeWidth = random.randint(500, 1000) #隨機的圖片寬度 | ||
gridSize = random.randint(50, 300) #網格大小 | ||
|
||
backgroundColor = (random.randint(0,255), random.randint(0,255), random.randint(0,255)) #背景顏色 | ||
|
||
img = np.zeros((imgSizeHeight, imgSizeWidth, 3), np.uint8) | ||
img[:,:] = backgroundColor[:] #圖片背景顏色 | ||
img_pil = Image.fromarray(img) | ||
draw = ImageDraw.Draw(img_pil) | ||
font = fonts[random.randint(0, len(fonts) - 1)] | ||
|
||
drawGrid(draw, (imgSizeHeight, imgSizeWidth), gridSize) | ||
target = drawText(draw, font, (imgSizeHeight, imgSizeWidth), gridSize) | ||
# drawLine(draw, (imgSizeHeight, imgSizeWidth)) | ||
# drawCircle(draw, (imgSizeHeight, imgSizeWidth)) | ||
|
||
img = np.array(img_pil) | ||
img = cv2.cvtColor(img,cv2.COLOR_RGB2BGR) | ||
return img, target | ||
def loadFonts(fontPath = "./fonts/"): | ||
fonts = [] | ||
for i in os.listdir(fontPath): | ||
for j in range(10, 50): | ||
try: | ||
|
||
temp = ImageFont.truetype(fontPath+i, j) | ||
except Exception as e: | ||
print(i, j) | ||
pass | ||
else: | ||
fonts.append(temp) | ||
return fonts | ||
def rotate(image, angle, center=None, scale=1.0): | ||
# 获取图像尺寸 | ||
(h, w) = image.shape[:2] | ||
|
||
# 若未指定旋转中心,则将图像中心设为旋转中心 | ||
if center is None: | ||
center = (w / 2, h / 2) | ||
|
||
# 执行旋转 | ||
M = cv2.getRotationMatrix2D(center, angle, scale) | ||
rotated = cv2.warpAffine(image, M, (w, h)) | ||
|
||
# 返回旋转后的图像 | ||
return rotated | ||
if __name__ == '__main__' : | ||
pathImgStore = "./test1/storeImg/" | ||
pathTargetStore = "./test1/storeTarget/" | ||
maxImgCount = 1000 | ||
fonts = loadFonts() | ||
for i in range(maxImgCount): | ||
print(i, "/", maxImgCount) | ||
img, target = createImg() | ||
# rotated = random.randint(0, 359) | ||
# img = rotate(img, rotated) | ||
# target = rotate(target, rotated) | ||
# cv2.imwrite(pathImgStore + str(i) + "_" + str(rotated) + ".png", img) | ||
# cv2.imwrite(pathTargetStore + str(i) + "_" +str(rotated) + ".png", target) | ||
|
||
cv2.imwrite(pathImgStore + str(i) + ".png", img) | ||
cv2.imwrite(pathTargetStore + str(i)+ ".png", target) |
Oops, something went wrong.