-
Notifications
You must be signed in to change notification settings - Fork 1
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
wangke
committed
Dec 7, 2019
1 parent
9841c1b
commit 4d80355
Showing
5 changed files
with
230 additions
and
36 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 |
---|---|---|
@@ -1,2 +1,4 @@ | ||
*.pth | ||
*.log | ||
*.log | ||
*.json | ||
imagesWithBox/ |
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 |
---|---|---|
@@ -1,28 +1,108 @@ | ||
import cv2 | ||
import tkinter as tk | ||
import json | ||
import json, glob | ||
import os | ||
|
||
window = tk.Tk() | ||
person = {0: 'wk', 1: 'dln', 2: 'kml', 3: 'zsy', 4: 'hh', 5: 'wly', 6: 'lch', 7: 'wzh', 8: 'ys'} | ||
|
||
window.title('Make DataSet') | ||
window.geometry('900x300') | ||
|
||
person = {0: 'wk', 1: 'dln', 2: 'kml', 3: 'zsy', 4: 'hh', 5: 'wly', 6: 'lch', 7: 'wzh', 8: 'ys'} | ||
def main(): | ||
window = tk.Tk() | ||
|
||
window.title('Make DataSet') | ||
window.geometry('900x300') | ||
|
||
imagesWithBox = './imagesWithBox' | ||
imagesList = os.listdir(imagesWithBox) | ||
|
||
labelsJson = './labels.json' | ||
|
||
if not os.path.exists(labelsJson): | ||
labels = {} | ||
else: | ||
f = open(labelsJson, 'r') | ||
labels = json.load(f) | ||
|
||
needHandleList = [] | ||
|
||
for path in imagesList: | ||
name = path.replace('.png', '') | ||
if name not in labels.keys(): | ||
needHandleList.append(path) | ||
|
||
if len(needHandleList) == 0: | ||
exit(0) | ||
|
||
index = 0 | ||
|
||
l = [0] * 12 | ||
|
||
img = cv2.imread(imagesWithBox + '/' + needHandleList[index]) | ||
cv2.imshow('Image', img) | ||
|
||
content = tk.StringVar() | ||
content.set("There are %d images need to handle" % len(needHandleList)) | ||
w = tk.Label(window, textvariable=content) | ||
w.pack() | ||
|
||
|
||
def onClick(data): | ||
global index, l | ||
if data == 'save': | ||
if index >= len(needHandleList): | ||
content.set(" " * 100) | ||
content.set("Finished!") | ||
return | ||
if l[0] == 0 and l[1] == 0 and l[2] == 0: | ||
return | ||
mark = False | ||
for i in l[3:]: | ||
if i == 1: | ||
mark = True | ||
if not mark: | ||
return | ||
labels[needHandleList[index].replace('.png', '')] = l | ||
with open(labelsJson, 'w') as file: | ||
json.dump(labels, file) | ||
|
||
index += 1 | ||
if index >= len(needHandleList): | ||
return | ||
l = [0] * 12 | ||
img = cv2.imread(imagesWithBox + '/' + needHandleList[index]) | ||
cv2.imshow('Image', img) | ||
elif data == 'come in': | ||
l[0] = 1 | ||
l[1] = 0 | ||
l[2] = 0 | ||
elif data == 'come out': | ||
l[0] = 0 | ||
l[1] = 1 | ||
l[2] = 0 | ||
elif data == 'stay': | ||
l[0] = 0 | ||
l[1] = 0 | ||
l[2] = 1 | ||
else: | ||
l[3 + int(data)] = 1 if l[3 + int(data)] == 0 else 0 | ||
if index < len(needHandleList): | ||
content.set( | ||
needHandleList[index].replace('.png', '') + " " + str(l) + '%d left' % (len(needHandleList) - index)) | ||
|
||
def onClick(data): | ||
print(data) | ||
|
||
b = tk.Button(window, text='come in', font=('Arial', 12), width=10, height=2, command=lambda: onClick('come in')) | ||
b.pack() | ||
b = tk.Button(window, text='come out', font=('Arial', 12), width=10, height=2, command=lambda: onClick('come out')) | ||
b.pack() | ||
b = tk.Button(window, text='stay', font=('Arial', 12), width=10, height=2, command=lambda: onClick('stay')) | ||
b.pack() | ||
b = tk.Button(window, text='save', font=('Arial', 12), width=10, height=2, command=lambda: onClick('save')) | ||
b.pack() | ||
for p in person: | ||
b = tk.Button(window, text=person[p], font=('Arial', 12), width=10, height=2, command=lambda p=p: onClick(p)) | ||
b.pack(side='left') | ||
|
||
b = tk.Button(window, text='come in', font=('Arial', 12), width=10, height=2, command=lambda: onClick('come in')) | ||
b.pack() | ||
b = tk.Button(window, text='come out', font=('Arial', 12), width=10, height=2, command=lambda: onClick('come out')) | ||
b.pack() | ||
b = tk.Button(window, text='stay', font=('Arial', 12), width=10, height=2, command=lambda: onClick('stay')) | ||
b.pack() | ||
b = tk.Button(window, text='save', font=('Arial', 12), width=10, height=2, command=lambda: onClick('save')) | ||
b.pack() | ||
for p in person: | ||
b = tk.Button(window, text=person[p], font=('Arial', 12), width=10, height=2, command=lambda: onClick(p)) | ||
b.pack(side='left') | ||
window.mainloop() | ||
|
||
window.mainloop() | ||
if __name__ == '__main__': | ||
main() |
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
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,72 @@ | ||
import torch | ||
from torchvision.transforms import Compose, ToTensor, Normalize | ||
from torchvision import models | ||
import time, glob | ||
import cv2 | ||
import numpy as np | ||
from model import ResNet50 | ||
import json | ||
from getTrainDataSet import person | ||
|
||
OUT_DIM = 12 | ||
|
||
resnet = models.resnet50() | ||
resnet.load_state_dict(torch.load('./resnet50-19c8e357.pth')) | ||
|
||
net = ResNet50(resnet, OUT_DIM) | ||
|
||
net.load_state_dict(torch.load('models/latestModel.pth')) | ||
|
||
trainDataPath = './imagesWithBox' | ||
allTrainDataList = glob.glob(trainDataPath + '/*.png') | ||
|
||
f = open('./labels.json', 'r') | ||
labels = json.load(f) | ||
|
||
transform = Compose([ToTensor(), Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225))]) | ||
|
||
|
||
def getStr(data): | ||
data -= 3 | ||
if data == -3: | ||
return 'come in, ' | ||
elif data == -2: | ||
return 'come out, ' | ||
elif data == -1: | ||
return 'stay, ' | ||
else: | ||
return person[data] | ||
|
||
|
||
for path in allTrainDataList: | ||
name = path.replace(trainDataPath, '').replace('.png', '')[1:] | ||
# print(name) | ||
imgO = cv2.imread(path) | ||
img = cv2.cvtColor(imgO, cv2.COLOR_BGR2RGB) | ||
img = transform(img).unsqueeze_(0) | ||
t1 = time.time() | ||
predLabel = net(img) | ||
gtLable = labels[name] | ||
print('use %.4f' % (time.time() - t1)) | ||
predictions = predLabel.data.cpu().numpy()[0] | ||
idx_list = np.where(predictions > np.percentile(predictions, 90))[0] | ||
# print(gtLable) | ||
gtStr = 'GT ' | ||
for i in range(len(gtLable)): | ||
if gtLable[i] == 1: | ||
gtStr += getStr(i) | ||
gtStr += ' ' | ||
print(gtStr) | ||
# print(idx_list) | ||
predStr = 'PD ' | ||
for i in idx_list: | ||
predStr += getStr(i) | ||
predStr += ' ' | ||
print(predStr) | ||
imgO = cv2.putText(imgO, gtStr, (10, 20), cv2.FONT_HERSHEY_SIMPLEX, | ||
0.7, (0, 0, 0), 1, cv2.LINE_AA) | ||
imgO = cv2.putText(imgO, predStr, (10, 50), cv2.FONT_HERSHEY_SIMPLEX, | ||
0.7, (0, 0, 0), 1, cv2.LINE_AA) | ||
cv2.imshow("image", imgO) | ||
cv2.waitKey(0) | ||
|
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