-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnailong_utils.py
46 lines (35 loc) · 1.06 KB
/
nailong_utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# nailong_utils.py
import torch
from PIL import Image
from torchvision import transforms
import pygame
from model.CNN import VGG
label_list = ["Relax, normal image", "Nailong!!! what the fuck"]
# 初始化 Pygame
pygame.init()
pygame.mixer.init()
# 初始化模型
def load_model():
model = VGG()
model.load_state_dict(torch.load("model/model_weights.pth"))
model.eval() # 切换到评估模式
return model
# 预处理图像
def preprocess_image(image_path):
preprocess = transforms.Compose([
transforms.Resize((224, 224)),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
])
image = Image.open(image_path).convert("RGB")
return preprocess(image).unsqueeze(0)
# 进行预测
def predict_image(model, image_tensor):
with torch.no_grad():
output = model(image_tensor)
_, predicted_idx = torch.max(output, 1)
return predicted_idx.item()
# 播放音乐
def play_music(music_file):
pygame.mixer.music.load(music_file)
pygame.mixer.music.play()