lovelyai999 commited on
Commit
77cd821
·
verified ·
1 Parent(s): baa6749

Create imageAI.py

Browse files
Files changed (1) hide show
  1. imageAI.py +108 -0
imageAI.py ADDED
@@ -0,0 +1,108 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ try:
2
+ import google.colab
3
+ IN_COLAB = True
4
+ from google.colab import drive,files
5
+ from google.colab import output
6
+ drive.mount('/gdrive')
7
+ Gbase="/gdrive/MyDrive/generate/"
8
+ cache_dir="/gdrive/MyDrive/hf/"
9
+ import sys
10
+ sys.path.append(Gbase)
11
+ except:
12
+ IN_COLAB = False
13
+ Gbase="./"
14
+ cache_dir="./hf/"
15
+
16
+
17
+ import cv2,os
18
+ import numpy as np
19
+ import random,string
20
+ import torch
21
+ import torch.nn as nn
22
+ import torch.nn.functional as F
23
+ import torch.optim as optim
24
+ from torch.utils.data import Dataset, DataLoader
25
+
26
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
27
+ print(f"Using device: {device}")
28
+
29
+ IMAGE_SIZE = 64
30
+ NUM_SAMPLES = 1000
31
+ BATCH_SIZE = 4
32
+ EPOCHS = 500
33
+ LEARNING_RATE = 0.001
34
+
35
+ class SimpleModel(nn.Module):
36
+ def __init__(self, path=None):
37
+ super(SimpleModel, self).__init__()
38
+ self.conv1 = nn.Conv2d(1, 32, 3, padding=1)
39
+ self.bn1 = nn.BatchNorm2d(32)
40
+ self.conv2 = nn.Conv2d(32, 64, 3, padding=1)
41
+ self.bn2 = nn.BatchNorm2d(64)
42
+ self.conv3 = nn.Conv2d(64, 128, 3, padding=1)
43
+ self.bn3 = nn.BatchNorm2d(128)
44
+ self.pool = nn.MaxPool2d(2, 2)
45
+ self.fc1 = nn.Linear(128 * 8 * 8, 512)
46
+ self.fc2 = nn.Linear(512, 128)
47
+ self.fc3 = nn.Linear(128, 1)
48
+ self.dropout = nn.Dropout(0.5)
49
+
50
+ if path and os.path.exists(path):
51
+ self.load_state_dict(torch.load(path, map_location=device))
52
+
53
+ def forward(self, x):
54
+ x = self.pool(F.leaky_relu(self.bn1(self.conv1(x))))
55
+ x = self.pool(F.leaky_relu(self.bn2(self.conv2(x))))
56
+ x = self.pool(F.leaky_relu(self.bn3(self.conv3(x))))
57
+ x = x.view(-1, 128 * 8 * 8)
58
+ x = F.leaky_relu(self.fc1(x))
59
+ x = self.dropout(x)
60
+ x = F.leaky_relu(self.fc2(x))
61
+ x = self.dropout(x)
62
+ x = self.fc3(x)
63
+ return x
64
+
65
+ def predict(self, image):
66
+ self.eval()
67
+ with torch.no_grad():
68
+ if isinstance(image, str) and os.path.isfile(image):
69
+ # 如果輸入是圖片文件路徑
70
+ img = cv2.imread(image, cv2.IMREAD_GRAYSCALE)
71
+ img = cv2.resize(img, (IMAGE_SIZE, IMAGE_SIZE))
72
+ elif isinstance(image, np.ndarray):
73
+ # 如果輸入是 numpy 數組
74
+ if image.ndim == 3:
75
+ img = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
76
+ else:
77
+ img = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
78
+ img = cv2.resize(img, (IMAGE_SIZE, IMAGE_SIZE))
79
+ else:
80
+ raise ValueError("Input should be an image file path or a numpy array")
81
+
82
+ img_tensor = torch.FloatTensor(img).unsqueeze(0).unsqueeze(0) / 255.0
83
+ img_tensor = img_tensor.to(device)
84
+ output = self(img_tensor).item()
85
+
86
+ # 將輸出四捨五入到最接近的整數
87
+ num_instructions = round(output)
88
+
89
+ # 生成相應數量的繪圖指令
90
+ instructions = []
91
+ for _ in range(num_instructions):
92
+ shape = random.choice(['line', 'rectangle', 'circle', 'ellipse', 'polygon'])
93
+ if shape == 'line':
94
+ instructions.append(f"cv2.line(image, {(random.randint(0, IMAGE_SIZE), random.randint(0, IMAGE_SIZE))}, {(random.randint(0, IMAGE_SIZE), random.randint(0, IMAGE_SIZE))}, {random.randint(0, 255)}, {random.randint(1, 3)})")
95
+ elif shape == 'rectangle':
96
+ instructions.append(f"cv2.rectangle(image, {(random.randint(0, IMAGE_SIZE-10), random.randint(0, IMAGE_SIZE-10))}, {(random.randint(10, IMAGE_SIZE), random.randint(10, IMAGE_SIZE))}, {random.randint(0, 255)}, {random.randint(1, 3)})")
97
+ elif shape == 'circle':
98
+ instructions.append(f"cv2.circle(image, {(random.randint(10, IMAGE_SIZE-10), random.randint(10, IMAGE_SIZE-10))}, {random.randint(5, 30)}, {random.randint(0, 255)}, {random.randint(1, 3)})")
99
+ elif shape == 'ellipse':
100
+ instructions.append(f"cv2.ellipse(image, {(random.randint(10, IMAGE_SIZE-10), random.randint(10, IMAGE_SIZE-10))}, {(random.randint(5, 30), random.randint(5, 30))}, {random.randint(0, 360)}, 0, 360, {random.randint(0, 255)}, {random.randint(1, 3)})")
101
+ elif shape == 'polygon':
102
+ num_points = random.randint(3, 6)
103
+ points = [(random.randint(0, IMAGE_SIZE), random.randint(0, IMAGE_SIZE)) for _ in range(num_points)]
104
+ instructions.append(f"cv2.polylines(image, [np.array({points})], True, {random.randint(0, 255)}, {random.randint(1, 3)})")
105
+
106
+
107
+ return instructions
108
+