!pip install pytz
from datetime import datetime
import pytz
from time import gmtime, strftime,sleep
import warnings
warnings.filterwarnings("ignore", category=DeprecationWarning)
import warnings
warnings.filterwarnings("ignore")
warnings.filterwarnings("ignore", category=DeprecationWarning)
warnings.filterwarnings("ignore", module="sklearn")
import warnings
# Menyembunyikan semua peringatan
warnings.filterwarnings("ignore")
# Menyembunyikan peringatan spesifik, misalnya UserWarning
warnings.filterwarnings("ignore", category=UserWarning)
jakarta_tz = pytz.timezone('Asia/Jakarta')
jakarta_time = datetime.now(jakarta_tz)
time_str = jakarta_time.strftime("%H:%M:%S")
print("Waktu di Indonesia/Jakarta:", time_str)
tgl=strftime("%Y-%m-%d", gmtime()) #u waktu
jam=strftime("%H:%M:%S", gmtime()) #u waktu
jakarta_tz = pytz.timezone('Asia/Jakarta')
jakarta_time = datetime.now(jakarta_tz)
jam = jakarta_time.strftime("%H:%M:%S")
+++++++++++++
Contoh penerapan :
def updateDB(cursor,db,idx):
sql = "UPDATE `tb_temp` set `status`='1',`status2`='1' where id='%s'" % (idx)
#print(sql)
v=0
try:
cursor.execute(sql)
db.commit()
v=1
except:
db.rollback()
return v
# In[3]:E:\xampp\htdocs\appPlatkendaraan\ypathfile
#pathme='E:\xampp\htdocs\appPlatkendaraan\ypathfile'
pathme='E:\\xampp\\htdocs\\appPlatkendaraan\\ypathfile'
sfr = SimpleFacerec()
AL=pathme+"/dataset/"
print (AL)
sfr.load_encoding_images(AL)
# In[ ]:
IMG=""
kon=True
db = MySQLdb.connect("localhost","root","","24_eparkir")
cursor = db.cursor()
while (kon):
ada=0
ID=''
gambar1=''
gambar2=''
NAME1=''
NAME2=''
try:
cursor.execute("SELECT `id`,`gambar1`,`gambar2` FROM `tb_temp` where `status`='0' order by `id` desc limit 0,1")
db.commit()
for row in cursor.fetchall():
ID=row[0]
gambar1=row[1]
gambar2=row[2]
ada=1
AL=pathme+'/'+gambar1
print("Face:"+AL)
frame= cv2.imread(r''+AL)
face_locations, face_names = sfr.detect_known_faces(frame)
for face_loc, name in zip(face_locations, face_names):
y1, x2, y2, x1 = face_loc[0], face_loc[1], face_loc[2], face_loc[3]
ar=name.split('-')
NAME1=ar[0]
print ('GET1='+NAME1)
#updateDB(cursor,db,ID)
#kon=False
###################################
AL=pathme+'/'+gambar2
print("Plat:"+AL)
img = cv.imread(r''+AL)
img = cv.resize(img, (int(img.shape[1]*.4),int(img.shape[0]*.4)))
img_gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
kernel = cv.getStructuringElement(cv.MORPH_ELLIPSE,(20,20))
img_opening = cv.morphologyEx(img_gray, cv.MORPH_OPEN, kernel)
img_norm = img_gray - img_opening
(thresh, img_norm_bw) = cv.threshold(img_norm, 0, 255, cv.THRESH_BINARY + cv.THRESH_OTSU)
(thresh, img_without_norm_bw) = cv.threshold(img_gray, 0, 255, cv.THRESH_BINARY + cv.THRESH_OTSU)
contours_vehicle, hierarchy = cv.findContours(img_norm_bw, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
index_plate_candidate = []
index_counter_contour_vehicle = 0
for contour_vehicle in contours_vehicle:
x,y,w,h = cv.boundingRect(contour_vehicle)
aspect_ratio = w/h
if w >= 200 and aspect_ratio <= 4 :
index_plate_candidate.append(index_counter_contour_vehicle)
index_counter_contour_vehicle += 1
img_show_plate = img.copy()
img_show_plate_bw = cv.cvtColor(img_norm_bw, cv.COLOR_GRAY2RGB)
if len(index_plate_candidate) == 0:
print("Plat nomor tidak ditemukan")
elif len(index_plate_candidate) == 1:
x_plate,y_plate,w_plate,h_plate = cv.boundingRect(contours_vehicle[index_plate_candidate[0]])
cv.rectangle(img_show_plate,(x_plate,y_plate),(x_plate+w_plate,y_plate+h_plate),(0,255,0),5)
cv.rectangle(img_show_plate_bw,(x_plate,y_plate),(x_plate+w_plate,y_plate+h_plate),(0,255,0),5)
img_plate_gray = img_gray[y_plate:y_plate+h_plate, x_plate:x_plate+w_plate]
else:
#print('Dapat dua lokasi plat, pilih lokasi plat kedua')
x_plate,y_plate,w_plate,h_plate = cv.boundingRect(contours_vehicle[index_plate_candidate[1]])
cv.rectangle(img_show_plate,(x_plate,y_plate),(x_plate+w_plate,y_plate+h_plate),(0,255,0),5)
cv.rectangle(img_show_plate_bw,(x_plate,y_plate),(x_plate+w_plate,y_plate+h_plate),(0,255,0),5)
img_plate_gray = img_gray[y_plate:y_plate+h_plate, x_plate:x_plate+w_plate]
(thresh, img_plate_bw) = cv.threshold(img_plate_gray, 0, 255, cv.THRESH_BINARY + cv.THRESH_OTSU)
kernel = cv.getStructuringElement(cv.MORPH_CROSS, (3,3))
img_plate_bw = cv.morphologyEx(img_plate_bw, cv.MORPH_OPEN, kernel) # apply morph open
contours_plate, hierarchy = cv.findContours(img_plate_bw, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
index_chars_candidate = [] #index
index_counter_contour_plate = 0 #idx
img_plate_rgb = cv.cvtColor(img_plate_gray,cv.COLOR_GRAY2BGR)
img_plate_bw_rgb = cv.cvtColor(img_plate_bw, cv.COLOR_GRAY2RGB)
for contour_plate in contours_plate:
x_char,y_char,w_char,h_char = cv.boundingRect(contour_plate)
if h_char >= 40 and h_char <= 60 and w_char >=10:
index_chars_candidate.append(index_counter_contour_plate)
cv.rectangle(img_plate_rgb,(x_char,y_char),(x_char+w_char,y_char+h_char),(0,255,0),5)
cv.rectangle(img_plate_bw_rgb,(x_char,y_char),(x_char+w_char,y_char+h_char),(0,255,0),5)
index_counter_contour_plate += 1
if index_chars_candidate == []:
print('Karakter tidak tersegmentasi')
else:
score_chars_candidate = np.zeros(len(index_chars_candidate))
counter_index_chars_candidate = 0
for chars_candidateA in index_chars_candidate:
xA,yA,wA,hA = cv.boundingRect(contours_plate[chars_candidateA])
for chars_candidateB in index_chars_candidate:
if chars_candidateA == chars_candidateB:
continue
else:
xB,yB,wB,hB = cv.boundingRect(contours_plate[chars_candidateB])
y_difference = abs(yA - yB)
if y_difference < 11:
score_chars_candidate[counter_index_chars_candidate] = score_chars_candidate[counter_index_chars_candidate] + 1
counter_index_chars_candidate += 1
index_chars = []
chars_counter = 0
for score in score_chars_candidate:
if score == max(score_chars_candidate):
index_chars.append(index_chars_candidate[chars_counter])
chars_counter += 1
x_coors = []
for char in index_chars:
x, y, w, h = cv.boundingRect(contours_plate[char])
x_coors.append(x)
x_coors = sorted(x_coors)
index_chars_sorted = []
for x_coor in x_coors:
for char in index_chars:
x, y, w, h = cv.boundingRect(contours_plate[char])
if x_coors[x_coors.index(x_coor)] == x:
index_chars_sorted.append(char)
num_plate = []
for char_sorted in index_chars_sorted:
x,y,w,h = cv.boundingRect(contours_plate[char_sorted])
char_crop = cv.cvtColor(img_plate_bw[y:y+h,x:x+w], cv.COLOR_GRAY2BGR)
char_crop = cv.resize(char_crop, (img_width, img_height))
img_array = keras.preprocessing.image.img_to_array(char_crop)
img_array = tf.expand_dims(img_array, 0)
predictions = model.predict(img_array)
score = tf.nn.softmax(predictions[0])
num_plate.append(class_names[np.argmax(score)])
#print(class_names[np.argmax(score)], end='')
plate_number = ''
for a in num_plate:
plate_number += a
print(plate_number)
NAME2=plate_number
print ('GET2='+NAME2)
updateDB(cursor,db,ID)
tgl=strftime("%Y-%m-%d", gmtime()) #u waktu
jam=strftime("%H:%M:%S", gmtime()) #u waktu
jakarta_tz = pytz.timezone('Asia/Jakarta')
jakarta_time = datetime.now(jakarta_tz)
jam = jakarta_time.strftime("%H:%M:%S")
sql2="INSERT INTO `tb_lewat` (`id_lewat`, `tanggal`, `jam`, `gambar_wajah`, `id_pemilik`, `status_wajah`, `gambar_nomor`, `id_kendaraan`, `status_kendaraan`, `status_kepemilikan`, `keterangan`, `temp1`, `temp2`) VALUES ('', '%s', '%s', '%s', '%s', '%s','%s', '%s', '%s', '%s', '%s', '%s', '%s')" % (tgl,jam,gambar1,NAME1,'Proses',gambar2,NAME2,'Proses','','',NAME1,NAME2)
print(sql2)
try:
cursor.execute(sql2)
db.commit()
except:
db.rollback()
except:
print('No data....')
cursor.close()
db.close()