Skip to content

Commit b552f6f

Browse files
committed
VER 1.0.0-beta.1
-# 添加用于增强 ImageNet 格式数据集的 Python 批处理脚本。 *G 修改 Git Ignore 文件。 -# 添加用于构建 Windows EXE 的 ICO 格式图标。 *- 显示实时视频流的图像为镜像模式(左右翻转)。 *< 源代码中缩进的 4 Spaces 被替换为 1 Tab。 ^- 增加了防止运行时目录影响程序文件读写的模块。 x- 在非调试模式中实时视频流窗口上不显示 68 个人脸关键点。
1 parent 4e3f71b commit b552f6f

5 files changed

Lines changed: 159 additions & 110 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
__pycache__/
22
.vscode/
3+
build/
34
learning_focus.dat
45
recent.json

ImageNet/resize.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
for index, file in enumerate(files):
1515
img_path = r_dir + file
1616
img = Image.open(img_path)
17-
resize = transforms.Resize([20, 20])
17+
resize = transforms.Resize([224, 224])
1818
IMG = resize(img)
1919
w_dir = new_dir + classes[cnt] + '\\'
2020
if not os.path.exists(w_dir):

ImageNet/resize_enhance.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import os
2+
from PIL import Image
3+
from torchvision import transforms
4+
5+
ext = '.jpg'
6+
rt_dir = os.getcwd() + '\\'
7+
classes = os.listdir(rt_dir + 'origin_set')
8+
read_dir = rt_dir + 'origin_set\\'
9+
new_dir = rt_dir + 'result_set\\'
10+
11+
for cnt in range(len(classes)):
12+
r_dir = read_dir + classes[cnt] + '\\'
13+
files = os.listdir(r_dir)
14+
for index, file in enumerate(files):
15+
img_path = r_dir + file
16+
img = Image.open(img_path)
17+
resize = transforms.Resize([224, 224])
18+
IMG1 = resize(img.transpose(Image.Transpose.FLIP_LEFT_RIGHT))
19+
IMG2 = resize(img.transpose(Image.Transpose.FLIP_TOP_BOTTOM))
20+
IMG3 = resize(img.transpose(Image.Transpose.ROTATE_90))
21+
IMG4 = resize(img.transpose(Image.Transpose.ROTATE_180))
22+
IMG5 = resize(img.transpose(Image.Transpose.ROTATE_270))
23+
w_dir = new_dir + classes[cnt] + '\\'
24+
if not os.path.exists(w_dir):
25+
os.makedirs(w_dir)
26+
save_path1 = w_dir + str(index * 5) + ext
27+
save_path2 = w_dir + str(index * 5 + 1) + ext
28+
save_path3 = w_dir + str(index * 5 + 2) + ext
29+
save_path4 = w_dir + str(index * 5 + 3) + ext
30+
save_path5 = w_dir + str(index * 5 + 4) + ext
31+
IMG1 = IMG1.convert('RGB')
32+
IMG2 = IMG2.convert('RGB')
33+
IMG3 = IMG3.convert('RGB')
34+
IMG4 = IMG4.convert('RGB')
35+
IMG5 = IMG5.convert('RGB')
36+
IMG1.save(save_path1)
37+
IMG2.save(save_path2)
38+
IMG3.save(save_path3)
39+
IMG4.save(save_path4)
40+
IMG5.save(save_path5)
41+
print(cnt)

learning_focus.ico

23.5 KB
Binary file not shown.

learning_focus.py

Lines changed: 116 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# Libraries
2+
import os
23
import cv2
34
import dlib
45
import json
@@ -7,121 +8,127 @@
78
import tkinter
89
import datetime
910

11+
# Variables
12+
DEBUG_MODE = False
13+
MAX_TRACKERS = 50
14+
BASE_DIR = os.path.dirname(__file__)
15+
1016
# Functions
1117
def show_plot_image_data(input: list):
12-
l1 = []
13-
l2 = []
14-
stick = []
15-
for time, totgreen, totred in input:
16-
stick.append(time)
17-
l1.append(totgreen)
18-
l2.append(totred)
19-
figure = plot.figure()
20-
figure.canvas.manager.window.title('Recent')
21-
figure.canvas.manager.window.iconphoto(False, tkinter.PhotoImage(file = 'learning_focus.png'))
22-
plot.title('Recent')
23-
plot.bar(numpy.arange(len(l1)), l1, width = 0.4, color = 'green', label = 'Good')
24-
for x, y in enumerate(l1):
25-
plot.text(x, y, y)
26-
plot.bar(numpy.arange(len(l2)) + 0.4, l2, width = 0.4, color = 'red', label = 'Bad')
27-
for x, y in enumerate(l2):
28-
plot.text(x + 0.4, y, y)
29-
plot.xticks(numpy.arange(len(input)) + 0.4 / 2, stick)
30-
plot.ylabel('Time (Minute)')
31-
plot.legend(loc = 'upper left')
32-
plot.show()
18+
l1 = []
19+
l2 = []
20+
stick = []
21+
for time, totgreen, totred in input:
22+
stick.append(time)
23+
l1.append(totgreen)
24+
l2.append(totred)
25+
figure = plot.figure()
26+
figure.canvas.manager.window.title('Recent')
27+
figure.canvas.manager.window.iconphoto(False, tkinter.PhotoImage(file = os.path.join(BASE_DIR, './learning_focus.png')))
28+
plot.title('Recent')
29+
plot.bar(numpy.arange(len(l1)), l1, width = 0.4, color = 'green', label = 'Good')
30+
for x, y in enumerate(l1):
31+
plot.text(x, y, y)
32+
plot.bar(numpy.arange(len(l2)) + 0.4, l2, width = 0.4, color = 'red', label = 'Bad')
33+
for x, y in enumerate(l2):
34+
plot.text(x + 0.4, y, y)
35+
plot.xticks(numpy.arange(len(input)) + 0.4 / 2, stick)
36+
plot.ylabel('Time (Minute)')
37+
plot.legend(loc = 'upper left')
38+
plot.show()
3339

3440
def save_total_time_data(time: str, totgreen: float, totred: float):
35-
try:
36-
with open('./recent.json', 'r', encoding = 'utf-8') as f:
37-
input = json.load(f)
38-
except (FileNotFoundError, json.decoder.JSONDecodeError):
39-
input = []
40-
with open('./recent.json', 'w', encoding = 'utf-8') as f:
41-
input.append([time, totgreen, totred])
42-
input = input[-3:]
43-
show_plot_image_data(input)
44-
json.dump(input, f)
41+
try:
42+
with open(os.path.join(BASE_DIR, './recent.json'), 'r', encoding = 'utf-8') as f:
43+
input = json.load(f)
44+
except (FileNotFoundError, json.decoder.JSONDecodeError):
45+
input = []
46+
with open(os.path.join(BASE_DIR, './recent.json'), 'w', encoding = 'utf-8') as f:
47+
input.append([time, totgreen, totred])
48+
input = input[-3:]
49+
show_plot_image_data(input)
50+
json.dump(input, f)
4551

4652
def main():
47-
cap = cv2.VideoCapture(0)
48-
time.sleep(1)
49-
detector = dlib.get_frontal_face_detector()
50-
predictor = dlib.shape_predictor('learning_focus.dat')
51-
trackers = [dlib.correlation_tracker() for _ in range(10)]
52-
status = 'green'
53-
totgreen = 0
54-
totred = 0
55-
while True:
56-
timestampstart = time.time()
57-
frame = cap.read()[1]
58-
gray = cv2.cvtColor(src = frame, code = cv2.COLOR_BGR2GRAY)
59-
faces = detector(gray)
60-
for k in range(len(faces)):
61-
trackers[k].start_track(frame, faces[k])
62-
trackers[k].update(frame)
63-
pos = trackers[k].get_position()
64-
cv2.rectangle(frame, (int(pos.left() - 20), int(pos.top() - 20)), (int(pos.right() + 20), int(pos.bottom() + 20)), ((0, 0, 255) if status == 'red' else (0, 255, 0)), 2)
65-
if len(faces) == 0:
66-
status = 'red'
67-
else:
68-
status = 'green'
69-
for face in faces:
70-
landmarks = predictor(image = gray, box = face)
71-
if 1.5 * (landmarks.part(37).y - landmarks.part(19).y) < (landmarks.part(8).y - landmarks.part(57).y):
72-
try:
73-
b, g, r = frame[int(landmarks.part(40).x - 1), int(landmarks.part(40).y - 3)]
74-
except IndexError:
75-
status = 'red'
76-
break
77-
if 0.11 * b + 0.59 * g + 0.3 * r >= 130:
78-
status = 'red'
79-
break
80-
if 2 * (landmarks.part(29).x - landmarks.part(1).x) < (landmarks.part(15).x - landmarks.part(29).x):
81-
try:
82-
b, g, r = frame[int(landmarks.part(42).x + 6), int(landmarks.part(42).y - 3)]
83-
except IndexError:
84-
status = 'red'
85-
break
86-
if 0.11 * b + 0.59 * g + 0.3 * r >= 100:
87-
status = 'red'
88-
break
89-
if (landmarks.part(29).x - landmarks.part(1).x) > (2 * (landmarks.part(15).x - landmarks.part(29).x)):
90-
try:
91-
b, g, r = frame[int(landmarks.part(42).x + 4), int(landmarks.part(39).y - 3)]
92-
except IndexError:
93-
status = 'red'
94-
break
95-
if 0.11 * b + 0.59 * g + 0.3 * r <= 100:
96-
status = 'red'
97-
break
98-
try:
99-
if (landmarks.part(40).y - landmarks.part(38).y > 10) and (landmarks.part(46).y - landmarks.part(44).y < 10):
100-
status = 'red'
101-
break
102-
except IndexError:
103-
status = 'red'
104-
break
105-
for n in range(68):
106-
x = landmarks.part(n).x
107-
y = landmarks.part(n).y
108-
cv2.circle(img = frame, center = (x, y), radius = 2, color = (255, 255, 255), thickness = -1)
109-
if status == 'green':
110-
totgreen += time.time() - timestampstart
111-
else:
112-
totred += time.time() - timestampstart
113-
cv2.imshow(winname = 'Face', mat = frame)
114-
if 0xFF & cv2.waitKey(1) == 27:
115-
break
116-
cap.release()
117-
cv2.destroyAllWindows()
118-
print('Total good studying time (Minute): ' + str(round(totgreen / 60, 2)))
119-
print('Total bad studying time (Minute): ' + str(round(totred / 60, 2)))
120-
save_total_time_data('{:04}-{:02}-{:02} {:02}:{:02}:{:02}'.format(datetime.datetime.now().year, datetime.datetime.now().month, datetime.datetime.now().day, datetime.datetime.now().hour, datetime.datetime.now().minute, datetime.datetime.now().second), round(totgreen / 60, 2), round(totred / 60, 2))
53+
cap = cv2.VideoCapture(0)
54+
time.sleep(1)
55+
detector = dlib.get_frontal_face_detector()
56+
predictor = dlib.shape_predictor(os.path.join(BASE_DIR, './learning_focus.dat'))
57+
trackers = [dlib.correlation_tracker() for _ in range(MAX_TRACKERS)]
58+
status = 'green'
59+
totgreen = 0
60+
totred = 0
61+
while True:
62+
timestampstart = time.time()
63+
frame = cap.read()[1]
64+
gray = cv2.cvtColor(src = frame, code = cv2.COLOR_BGR2GRAY)
65+
faces = detector(gray)
66+
for k in range(len(faces)):
67+
trackers[k].start_track(frame, faces[k])
68+
trackers[k].update(frame)
69+
pos = trackers[k].get_position()
70+
cv2.rectangle(frame, (int(pos.left() - 20), int(pos.top() - 20)), (int(pos.right() + 20), int(pos.bottom() + 20)), ((0, 0, 255) if status == 'red' else (0, 255, 0)), 2)
71+
if len(faces) == 0:
72+
status = 'red'
73+
else:
74+
status = 'green'
75+
for face in faces:
76+
landmarks = predictor(image = gray, box = face)
77+
if 1.5 * (landmarks.part(37).y - landmarks.part(19).y) < (landmarks.part(8).y - landmarks.part(57).y):
78+
try:
79+
b, g, r = frame[int(landmarks.part(40).x - 1), int(landmarks.part(40).y - 3)]
80+
except IndexError:
81+
status = 'red'
82+
break
83+
if 0.11 * b + 0.59 * g + 0.3 * r >= 130:
84+
status = 'red'
85+
break
86+
if 2 * (landmarks.part(29).x - landmarks.part(1).x) < (landmarks.part(15).x - landmarks.part(29).x):
87+
try:
88+
b, g, r = frame[int(landmarks.part(42).x + 6), int(landmarks.part(42).y - 3)]
89+
except IndexError:
90+
status = 'red'
91+
break
92+
if 0.11 * b + 0.59 * g + 0.3 * r >= 100:
93+
status = 'red'
94+
break
95+
if (landmarks.part(29).x - landmarks.part(1).x) > (2 * (landmarks.part(15).x - landmarks.part(29).x)):
96+
try:
97+
b, g, r = frame[int(landmarks.part(42).x + 4), int(landmarks.part(39).y - 3)]
98+
except IndexError:
99+
status = 'red'
100+
break
101+
if 0.11 * b + 0.59 * g + 0.3 * r <= 100:
102+
status = 'red'
103+
break
104+
try:
105+
if (landmarks.part(40).y - landmarks.part(38).y > 10) and (landmarks.part(46).y - landmarks.part(44).y < 10):
106+
status = 'red'
107+
break
108+
except IndexError:
109+
status = 'red'
110+
break
111+
if DEBUG_MODE == True:
112+
for n in range(68):
113+
x = landmarks.part(n).x
114+
y = landmarks.part(n).y
115+
cv2.circle(img = frame, center = (x, y), radius = 2, color = (255, 255, 255), thickness = -1)
116+
if status == 'green':
117+
totgreen += time.time() - timestampstart
118+
else:
119+
totred += time.time() - timestampstart
120+
cv2.imshow(winname = 'Face', mat = cv2.flip(frame, 1))
121+
if 0xFF & cv2.waitKey(1) == 27:
122+
break
123+
cap.release()
124+
cv2.destroyAllWindows()
125+
print('Total good studying time (Minute): ' + str(round(totgreen / 60, 2)))
126+
print('Total bad studying time (Minute): ' + str(round(totred / 60, 2)))
127+
save_total_time_data('{:04}-{:02}-{:02} {:02}:{:02}:{:02}'.format(datetime.datetime.now().year, datetime.datetime.now().month, datetime.datetime.now().day, datetime.datetime.now().hour, datetime.datetime.now().minute, datetime.datetime.now().second), round(totgreen / 60, 2), round(totred / 60, 2))
121128

122129
# Main
123130
if __name__ == '__main__':
124-
from matplotlib import use as usebackend
125-
usebackend('TkAgg')
126-
from matplotlib import pyplot as plot
127-
main()
131+
from matplotlib import use as usebackend
132+
usebackend('TkAgg')
133+
from matplotlib import pyplot as plot
134+
main()

0 commit comments

Comments
 (0)