-
Notifications
You must be signed in to change notification settings - Fork 0
/
grabcutloadalldata.py
395 lines (316 loc) · 9.25 KB
/
grabcutloadalldata.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
# -*- coding: utf-8 -*-
"""grabcutloadalldata.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1N6ppuAJttBGHwIvN8cUPGhU4TI3z8HkC
"""
from glob import glob
import cv2
from cv2 import imshow
import numpy as np
import cv2
from matplotlib import pyplot as plt
import PIL
"""Mencoba mengunakan salah satu gambar sawah.
---
Mengambil nilai intesitas RGB selanjutnya mengambil nilai mean pada warna hijau.
"""
#kode percobaan
img = cv2.imread('/content/drive/MyDrive/HSC/dataset/data training baru/20220218_153358.jpg')
# Metode Grabcut
# mask =np.zeros(img.shape[:2],np.uint8)
# bgdModel = np.zeros((1,65),np.float64)
# fgdModel = np.zeros((1,65),np.float64)
# rect= (40,40,50,300)
# cv2.grabCut(img,mask,rect,bgdModel,fgdModel,5,cv2.GC_INIT_WITH_RECT)
# mask2 =np.where((mask==2) | (mask==0),0,1).astype('uint8')
# img =img*mask2[:,:,np.newaxis]
# split nilai RGB
b,g,r = cv2.split(img)
img = cv2.merge((b,g,r))
print(f"nilai Aray gamnbar hijau: {g}")
print(f"nilai mean gambar hijau: {cv2.mean(g)}")
# print(f"nilai biru: {b}")
# plt.imshow(img),plt.colorbar(),plt.show()
print(f'nilai mean RGB :{cv2.mean(img)}')
# Gambar sawah
for im in glob('/content/drive/MyDrive/HSC/dataset/*.jpg'):
img = cv2.imread(im, -1)
print(cv2.mean(img))
"""**Data sawah**"""
i = 1
for im in glob('/content/drive/MyDrive/HSC/dataset/data gambar BWD/*.png'):
img = cv2.imread(im, -1)
i= i +1
print(f'level {i -1} :{cv2.mean(img)[1]}')
"""
* level 1 :171.0
* level 2 :156.0
* level 3 :145.0
* level 4 :130.0
* level 5 :118.0
* level 6 :104.0
* level 7 :92.0
* level 8 :80.0
---
Setelah didapatkan nilai intesitas warna hijau pada bagan warna (BWD) maka kami mengambil nilai rata dari kedua setiap level, misalnya nilai rata-rata **level 1** dan **level 2** maka didapatkan nilai **level A**.
* level A = {79.0, 163, 132}
* level B = {70.5, 137.5, 92,55}
* level c = {62.5, 111, 52.5}
* level d = {55.5, 86,15}
---
lalu diambil nilai tengahnya dikarenakan nilai tersebut merupakan nilai intesitas hijau.
* level A = 163.5
* level B = 137.5
* level c = 111
* level d = 86
"""
#percobaan split data
level_1=[]
level_2=[]
level_3=[]
level_4=[]
level_5=[]
level_6=[]
level_7=[]
level_8=[]
for im in glob('/content/drive/MyDrive/HSC/dataset/data training baru/*.*'):
img = cv2.imread(im, -1)
if cv2.mean(img)[1] >= 0:
if cv2.mean(img)[1] <= 80:
# level 8 atau level d
level_8.append(cv2.mean(img)[1])
elif cv2.mean(img)[1] <= 92:
# level 7 atau level c
level_7.append(cv2.mean(img)[1])
elif cv2.mean(img)[1] <= 104:
# level 6 atau level c
level_6.append(cv2.mean(img)[1])
elif cv2.mean(img)[1] <= 118:
# level 5 atau level c
level_5.append(cv2.mean(img)[1])
elif cv2.mean(img)[1] <= 130:
# level 4 atau level c
level_4.append(cv2.mean(img)[1])
elif cv2.mean(img)[1] <= 145:
# level 3 atau level c
level_3.append(cv2.mean(img)[1])
elif cv2.mean(img)[1] <= 156:
# level 2 atau level B
level_2.append(cv2.mean(img)[1])
else:
# level 1 level A
level_1.append(cv2.mean(img)[1])
else:
print("nilai dibawah 0")
# print(cv2.mean(img)[1])
# intesitasHijau.append(cv2.mean(img)[1])
print(level_8)
print(level_7)
print(level_6)
print(level_5)
print(level_4)
print(level_3)
print(level_2)
print(level_1)
#percobaan split data
#HSV
level_1=[]
level_2=[]
level_3=[]
level_4=[]
level_5=[]
level_6=[]
level_7=[]
level_8=[]
for im in glob('/content/drive/MyDrive/HSC/dataset/18-02-22/*.*'):
img = cv2.imread(im, -1)
# # result = img.copy()
# img = cv2.cvtColor(img, cv2.COLOR_BGR2HSV, (500, 500))
# # nilai warna pixel hijau
# lower2 = np.array([40,100,100])
# upper2 = np.array([70,255,255])
# upper_mask = cv2.inRange(img, lower2, upper2)
# full_mask2 = upper_mask;
# img = cv2.bitwise_and(img, img, mask=full_mask2)
if cv2.mean(img)[1] >= 1:
if cv2.mean(img)[1] <= 80:
# level 8 atau level d
level_8.append(cv2.mean(img)[1])
elif cv2.mean(img)[1] <= 92:
# level 7 atau level c
level_7.append(cv2.mean(img)[1])
elif cv2.mean(img)[1] <= 104:
# level 6 atau level c
level_6.append(cv2.mean(img)[1])
elif cv2.mean(img)[1] <= 118:
# level 5 atau level c
level_5.append(cv2.mean(img)[1])
elif cv2.mean(img)[1] <= 130:
# level 4 atau level c
level_4.append(cv2.mean(img)[1])
elif cv2.mean(img)[1] <= 145:
# level 3 atau level c
level_3.append(cv2.mean(img)[1])
elif cv2.mean(img)[1] <= 156:
# level 2 atau level B
level_2.append(cv2.mean(img)[1])
else:
# level 1 level A
level_1.append(cv2.mean(img)[1])
else:
print("nilai dibawah 0")
# print(cv2.mean(img)[1])
# intesitasHijau.append(cv2.mean(img)[1])
print(level_8)
print(level_7)
print(level_6)
print(level_5)
print(level_4)
print(level_3)
print(level_2)
print(level_1)
# Percobaan dengan data pada folder 18-02-22
#percobaan split data
intesitasHijau = []
level_1=[]
level_2=[]
level_3=[]
level_4=[]
for im in glob('/content/drive/MyDrive/HSC/dataset/18-02-22/*.*'):
img = cv2.imread(im, -1)
if cv2.mean(img)[1] >= 0:
if cv2.mean(img)[1] <= 86:
# level 4
level_4.append(cv2.mean(img)[1])
elif cv2.mean(img)[1] <= 111:
# level 3
level_3.append(cv2.mean(img)[1])
elif cv2.mean(img)[1] <= 137:
# level 2
level_2.append(cv2.mean(img)[1])
else:
# level 1
level_1.append(cv2.mean(img)[1])
else:
print("nilai dibawah 0")
# print(cv2.mean(img)[1])
# intesitasHijau.append(cv2.mean(img)[1])
print(level_4)
print(level_3)
print(level_2)
print(level_1)
# no camera
#percobaan split data
intesitasHijau = []
level_1=[]
level_2=[]
level_3=[]
level_4=[]
img = cv2.imread('/content/drive/MyDrive/HSC/dataset/data training baru/20220304_161043.jpg')
#grabcut
# mask =np.zeros(img.shape[:2],np.uint8)
# bgdModel = np.zeros((1,65),np.float64)
# fgdModel = np.zeros((1,65),np.float64)
# rect= (40,-40,50,300)
# cv2.grabCut(img,mask,rect,bgdModel,fgdModel,5,cv2.GC_INIT_WITH_RECT)
# mask2 =np.where((mask==2) | (mask==0),0,1).astype('uint8')
# img =img*mask2[:,:,np.newaxis]
# menentukan level
if cv2.mean(img)[1] >= 0:
if cv2.mean(img)[1] <= 86:
# level 4
level_4.append(cv2.mean(img)[1])
elif cv2.mean(img)[1] <= 111:
# level 3
level_3.append(cv2.mean(img)[1])
elif cv2.mean(img)[1] <= 137:
# level 2
level_2.append(cv2.mean(img)[1])
else:
# level 1
level_1.append(cv2.mean(img)[1])
else:
print("nilai dibawah 0")
print(f'Level 4:{level_4}')
print(f'level 3:{level_3}')
print(f'level 2:{level_2}')
print(f'level 1:{level_1}')
"""**Mencoba mengakses kamera secara realtime**"""
from IPython.display import display, Javascript
from google.colab.output import eval_js
from base64 import b64decode
def take_photo(filename='photo.jpg', quality=0.8):
js = Javascript('''
async function takePhoto(quality) {
const div = document.createElement('div');
const capture = document.createElement('button');
capture.textContent = 'Capture';
div.appendChild(capture);
const video = document.createElement('video');
video.style.display = 'block';
const stream = await navigator.mediaDevices.getUserMedia({video: true});
document.body.appendChild(div);
div.appendChild(video);
video.srcObject = stream;
await video.play();
// Resize the output to fit the video element.
google.colab.output.setIframeHeight(document.documentElement.scrollHeight, true);
// Wait for Capture to be clicked.
await new Promise((resolve) => capture.onclick = resolve);
const canvas = document.createElement('canvas');
canvas.width = video.videoWidth;
canvas.height = video.videoHeight;
canvas.getContext('2d').drawImage(video, 0, 0);
stream.getVideoTracks()[0].stop();
div.remove();
return canvas.toDataURL('image/jpeg', quality);
}
''')
display(js)
data = eval_js('takePhoto({})'.format(quality))
binary = b64decode(data.split(',')[1])
with open(filename, 'wb') as f:
f.write(binary)
return filename
"""**Mengambil gambar**
Disimpan pada var *img*
"""
from IPython.display import Image
level_1=[]
level_2=[]
level_3=[]
level_4=[]
try:
filename = take_photo()
print('Saved to {}'.format(filename))
# Show the image which was just taken.
display(Image(filename))
# membaca gambar yang telah diambil oleh kamera
img=cv2.imread('/content/photo.jpg')
# print nilai RGB mean gambar
# menentukan level
if cv2.mean(img)[1] >= 0:
if cv2.mean(img)[1] <= 86:
# level 4
level_4.append(cv2.mean(img)[1])
elif cv2.mean(img)[1] <= 111:
# level 3
level_3.append(cv2.mean(img)[1])
elif cv2.mean(img)[1] <= 137:
# level 2
level_2.append(cv2.mean(img)[1])
else:
# level 1
level_1.append(cv2.mean(img)[1])
else:
print("nilai dibawah 0")
print(f'Level 4:{level_4}')
print(f'level 3:{level_3}')
print(f'level 2:{level_2}')
print(f'level 1:{level_1}')
print(cv2.mean(img))
except Exception as err:
# Errors will be thrown if the user does not have a webcam or if they do not
# grant the page permission to access it.
print(str(err))