Related
I am trying to call my Face Recognition model implemented in keras, using flask API. I am unable to call the model using different cam urls as a parameter.
I am getting the following error:
TypeError: Cannot interpret feed_dict key as Tensor: Tensor Tensor("Placeholder_50:0", shape=(3, 3, 3, 32), dtype=float32) is not an element of this graph.
127.0.0.1 - - [23/Nov/2022 13:39:49] "GET /api/recognise?url=rtsp://admin:inndata123#10.10.5.202:554/cam/realmonitor?channel=1&subtype=0 HTTP/1.1" 500 -
I found that creating a new session for each thread, but I don't have any idea where to place those lines in my code.
# running db and email functions in background and parallalized action and bbox dist loops
import json
import os
import pickle
import cv2
import imutils
import dlib
import torch
import time
import numpy as np
import datetime
from pathlib import Path
import matplotlib.pyplot as plt
from PIL import Image, ImageFont, ImageDraw
from script.fx import prewhiten, l2_normalize
from keras.models import load_model
from scipy.spatial import distance
from mtcnn.mtcnn import MTCNN
from script.generate_data import generate_embeddings
import mysql.connector
from mysql.connector import (connection)
import smtplib
import mimetypes
from email.message import EmailMessage
message = EmailMessage()
import tensorflow as tf
session_conf = tf.ConfigProto(intra_op_parallelism_threads=1,
inter_op_parallelism_threads=2)
from flask import Flask, jsonify, request,render_template,Response
app = Flask(__name__)
global graph
graph = tf.get_default_graph()
sess = tf.Session(graph=graph, config=session_conf)
model_path = './data/model/facenet_keras.h5'
font_path = './data/font/Calibri Regular.ttf'
embedding_path = './data/arrays/embeddings.npz'
vars_path = './data/arrays/vars.npz'
curr_time = datetime.datetime.now()
time_date = curr_time.strftime('%Y-%m-%d %H:%M:%S')
only_date= curr_time.strftime('%Y-%m-%d')
login_time = curr_time.replace(hour=8, minute=0, second=0, microsecond=0)
logout_time = curr_time.replace(hour=17, minute=15, second=0, microsecond=0)
if os.path.exists(embedding_path) == True:
print('Loadings embeddings...')
loaded_embeddings = np.load(embedding_path)
embeddings, names = loaded_embeddings['a'], loaded_embeddings['b']
loaded_vars = np.load(vars_path)
slope, intercept = loaded_vars['a'], loaded_vars['b']
else:
print('Creatings embeddings...')
generate_embeddings()
loaded_embeddings = np.load(embedding_path)
embeddings, names = loaded_embeddings['a'], loaded_embeddings['b']
loaded_vars = np.load(vars_path)
slope, intercept = loaded_vars['a'], loaded_vars['b']
location='IE'
cam_id='Entrance-Cam'
frame_count = 0
frame_number = 0
bbox_centers = []
log_in = []
log_out = []
date_list = []
mins_lst = []
#app.route('/api/recognise')
def recognise():
url = request.args.get('url')
if url!=str(0):
subtype=request.args.get('subtype')
url=url+'&'+'subtype='+subtype
print(url)
else:url=int(url)
video_sources = cv2.VideoCapture(url)
detector = MTCNN()
model = load_model(model_path, compile=False)
graph = tf.get_default_graph()
def inner():
frame_count = 0
frame_number = 0
while 1:
start= time.time()
var, frame = video_sources.read()
if frame is not None:
small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)
# Convert the image from BGR color (which OpenCV uses) to RGB color (which face_recognition uses)
rgb_small_frame = small_frame[:, :, ::-1]
# frame = cv2.resize(frame, (1500, 1000))
if frame_count % 10 == 0 and rgb_small_frame is not None:
faces = detector.detect_faces(rgb_small_frame) # result
#print(faces)
print('faces :',len(faces))
for result in faces:
x_face, y_face, w_face, h_face = result['box']
x_face = x_face * 4
y_face = y_face * 4
w_face = w_face * 4
h_face = h_face * 4
x_face2=w_face+x_face
y_face2=h_face+y_face
#face bbox tuples
face_tuple1=(x_face,y_face)
face_tuple2=(x_face2,y_face2)
#zone bbox tuples
zone_tuple1 = (950, 700)
zone_tuple2 = (2000, 1050)
# Margins for Face box
dw = 0.1 * w_face
dh = 0.2 * h_face
#center = (x_face + w_face // 2, y_face + h_face // 2)
#cv2.rectangle(frame, zone_tuple1, zone_tuple2, (255, 0, 0), 2)
#if (all(x > y for x, y in zip(face_tuple1, zone_tuple1)))==True and (all(x < y for x, y in zip(face_tuple2, zone_tuple2)))==True:
# radius=2
with graph.as_default():
dist = []
for i in range(len(embeddings)):
dist.append(distance.euclidean(l2_normalize(model.predict(prewhiten(
cv2.resize(frame[y_face:y_face + h_face, x_face:x_face + w_face], (160, 160)).reshape(
-1, 160,
160,
3)))),
embeddings[i].reshape(1, 128)))
dist = np.array(dist)
if os.path.exists(only_date + '.txt') == False:
f = open(only_date + '.txt', "a+")
log_in.clear()
log_out.clear()
else:
if dist.min() > 1.20:
log = 'Unauthorized Entry'
emp_id = 'None'
f1 = open("unauthorised.txt", "a")
f1.writelines(f"\n{cam_id},{time_date},{log}")
elif dist.min() <= 1:
emp_id = names[dist.argmin()]
if int(emp_id) not in log_in and curr_time >= login_time:
log = 'punch-in'
f2 = open(only_date + '.txt', "a")
f2.writelines(f"\n{cam_id},{emp_id},{time_date},{log}")
f2.close()
log_in.append(int(emp_id))
print(log_in)
if int(emp_id) in log_in and curr_time >= logout_time and int(emp_id) not in log_out:
# and center[0] > 750 and center[0] > 960:
log = 'punch-out'
f2 = open(only_date + '.txt', "a")
f2.writelines(f"\n{cam_id},{emp_id},{time_date},{log}")
f2.close()
log_out.append(int(emp_id))
else:
emp_id = 'None'
log = 'unidentified'
if emp_id != 'unauthorized' and emp_id != 'unidentified':
font_size = int(
slope[dist.argmin()] * ((w_face + 2 * dw) // 3) * 2 + intercept[dist.argmin()])
color = (0, 255, 0)
elif emp_id == 'unauthorized':
font_size = int(
slope[dist.argmin()] * ((w_face + 2 * dw) // 3) * 2 + intercept[dist.argmin()])
color = (0, 0, 255)
else:
font_size = int(0.1974311 * ((w_face + 2 * dw) // 3) * 2 + 0.03397702412218706)
color = (0, 255, 0)
font = ImageFont.truetype(font_path, font_size)
size = font.getbbox(emp_id)
cv2.rectangle(frame,
pt1=(x_face - int(np.floor(dw)), (y_face - int(np.floor(dh)))),
pt2=(
(x_face + w_face + int(np.ceil(dw))), (y_face + h_face + int(np.ceil(dh)))),
color=(0, 255, 0),
thickness=2) # Face Rectangle
cv2.rectangle(frame,
pt1=(x_face - int(np.floor(dw)), y_face - int(np.floor(dh)) - size[1]),
pt2=(x_face + size[0], y_face - int(np.floor(dh))),
color=(0, 255, 0),
thickness=-1)
img = Image.fromarray(frame)
draw = ImageDraw.Draw(img)
draw.text((x_face - int(np.floor(dw)), y_face - int(np.floor(dh)) - size[1]), emp_id,
font=font,
fill=color)
frame = np.array(img)
if emp_id == 'unauthorized':
frame_name = f'{emp_id}_{frame_number}.jpg'
cv2.imwrite(f'data/unauthorized_faces/{frame_name}',
cv2.resize(frame[y_face:y_face + h_face, x_face:x_face + w_face],
(250, 250)))
elif emp_id != 'unauthorised' and emp_id != 'unidentified':
frame_name = f'{emp_id}_{frame_number}.jpg'
cv2.imwrite(f'data/detected_faces/{frame_name}',
cv2.resize(frame[y_face:y_face + h_face, x_face:x_face + w_face],
(250, 250)))
# add_attachment(frame_name)
frame_number += 1
end = time.time()
print(end-start)
print(emp_id)
if log != 'unidentified':
data = {'emp_id': emp_id, 'date': time_date, 'log': log}
yield json.dumps(data) + "\n"
# cv2.imshow('Frame', cv2.resize(frame, (950, 950)))
if cv2.waitKey(15) & 255 == ord('q'):
break
else:
continue
return Response(inner(), mimetype='application/json')
if __name__=='__main__':
app.run(host="0.0.0.0",threaded=True)
This is my face recognition model integrated in flask.
roi = [[(284, 764), (996, 840), 'text', 'name'],
[(1560, 756), (2312, 836), 'text', 'cnic'],
[(2000, 704), (2060, 748), 'box', 'corporate'],
[(2296, 696), (2360, 756), 'box', 'individual'],
[(1220, 844), (2360, 920), 'text', 'email']]
Above are the selections where I run tesseract if it is text, if it is a box then get '0' or '1' and want to save this to a dataframe which then can be saved to Excel with desired output (of column headers taken from last column of 'roi' above and values taken from output of tesseract and box values (1, or 0).
myPicList = os.listdir(sof_folder)
for j, y in enumerate(myPicList):
if 'SOF' in y:
img = cv.imread(sof_folder + "/" + y)
df = pd.DataFrame()
pixelThreshold = 1100
for x, r in enumerate(roi):
section = img[r[0][1]:r[1][1], r[0][0]:r[1][0]]
if len(df.columns) < len(roi):
if r[2] == 'text':
df[r[3]] = tess.image_to_string(section)
if r[2] == 'box':
imgGray = cv.cvtColor(section, cv.COLOR_BGR2GRAY)
imgThresh = cv.threshold(imgGray, 170, 255, cv.THRESH_BINARY_INV)[1]
totalPixels = cv.countNonZero(imgThresh)
if totalPixels > pixelThreshold: totalPixels = 1;
else: totalPixels = 0
df[r[3]] = totalPixels
df.to_excel('forms saved.xlsx')
However, it only returns the column names (i.e., name, cnic, email etc).
Shorter version of the code to be seen easily
for x, r in enumerate(roi):
section = img[r[0][1]:r[1][1], r[0][0]:r[1][0]]
if r[2] == 'text':
df[r[3]] = tess.image_to_string(section)
I tried both solutions from here but none worked for me. Second solution is just not working and first one gives weird output as [![one line contains only one tesseract output and only last image's output is retained]
My code edit for this is as follows:
d1 = {}
d = {}
results = []
df = pd.DataFrame(data=d1)
for x, r in enumerate(roi):
section = img[r[0][1]:r[1][1], r[0][0]:r[1][0]]
if len(df.columns) < len(roi):
if r[2] == 'text':
# df[r[3]] = tess.image_to_string(section)
readings = tess.image_to_string(section)
d = {r[3]: [readings]}
df = pd.DataFrame(data=d)
results.append(df)
if r[2] == 'box':
imgGray = cv.cvtColor(section, cv.COLOR_BGR2GRAY)
imgThresh = cv.threshold(imgGray, 170, 255, cv.THRESH_BINARY_INV)[1]
totalPixels = cv.countNonZero(imgThresh)
if totalPixels > pixelThreshold: totalPixels = 1;
else: totalPixels = 0
df[r[3]] = totalPixels
d = {r[3]: [totalPixels]}
df = pd.DataFrame(data=d)
results.append(df)
final_df = pd.concat(results, axis=0)
final_df.to_csv("final.csv")
df = pd.DataFrame()
for j, y in enumerate(myPicList):
if 'SOF' in y:
with open('dataOutput.csv', 'a+') as f:
f.write(y + ',')
img = cv.imread(sof_folder + "/" + y)
pixelThreshold = 1100
myData = []
for x, r in enumerate(roi):
section = img[r[0][1]:r[1][1], r[0][0]:r[1][0]]
if len(df.columns) < len(roi):
if r[2] == 'text':
text = tess.image_to_string(section)
text = text.replace("\n", " ")
myData.append(text)
if r[2] == 'box':
imgGray = cv.cvtColor(section, cv.COLOR_BGR2GRAY)
imgThresh = cv.threshold(imgGray, 170, 255, cv.THRESH_BINARY_INV)[1]
totalPixels = cv.countNonZero(imgThresh)
if totalPixels > pixelThreshold: totalPixels = 1;
else: totalPixels = 0
myData.append(totalPixels)
with open('dataOutput.csv', 'a+') as f:
for data in myData:
f.write((str(data)+','))
f.write('\n')
Not exactly stored in a dataframe but I hope this works for you.
I am trying to download images using this code. However, The website comes up with a captcha. When I try to select the captcha it displays a broken PC image. Cannot figure a way past this. Is their a way to avoid the captcha altogether? Or select it somehow for clicking options via selenium. It's a long code but an MRE.
from selenium import webdriver
from bs4 import BeautifulSoup as soup
from datetime import date ,timedelta
import requests
import time
import base64
import cv2
import pytesseract
import xlsxwriter
import numpy as np
import pandas as pd
import os
import shutil
driver = webdriver.Chrome("chromedriver")
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'
excel_name = ['Сите','Упис на основање','2б Ново со спојување на __','3б Ново со раздвојување од __','5б Ново со издвојување од__','Упис на промена','Документ за корекција','1б Присоединување во __','4б Превземање со раздвојување на __','5а Издвојување во ново__','6б Превземање од__','6а Издвојување во __','Документ за регистрирање на работно време','Документ за определување на главна приходна шифра и големина','Документ за евидентирање на казна/санкција','Документ за евидентирање на бришење на казна/санкција','Документ за евидентирање на стечај на друг субјект','Документ за евидентирање на заклучување на стечај на друг субјект','Упис на бришење','2а Спојување во ново__со бришење','4а Раздвојување со превземање во __ со бришење','3а Раздвојување на ново __ со бришење','1а Присоединување на __ со бришење','Судска Процедура - Стечај','Ликвидација','Претстечај (Претходна постапка)','Објава(Друго)','Објава(Стечајна постапка)','Објава(Ликвидациона постапка)','Вонсудска спогодба','Објава(Вонсудска спогодба)','Предбелешка']
#excel_name = ['Сите','Упис на основање','2б Ново со спојување на __','Упис на промена']
image_name = ['image', 'image0', 'image1', 'image2', 'image3', 'image4', 'image5', 'image6', 'image7', 'image8', 'image9', 'image10', 'image11', 'image12', 'image13', 'image14', 'image15', 'image16', 'image17', 'image18', 'image19', 'image20', 'image21', 'image22', 'image23', 'image24', 'image25', 'image26', 'image27', 'image28', 'image29', 'image30']
def get_text(data_number, image_name, excel_name):
workbook = xlsxwriter.Workbook(str(date.today() - timedelta(days=1)) + '-' + excel_name + '.xlsx')
worksheet = workbook.add_worksheet("content")
row = 0
print(image_name, data_number)
# Load image, grayscale, and Otsu's threshold
for i in range(data_number):
print('./images/' + str(date.today() - timedelta(days=3)) + '-' + image_name + str(i) + '.png')
image = cv2.imread('./images/' + str(date.today() - timedelta(days=3)) + '-' + image_name + str(i) + '.png')
try:
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
except:
continue
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
# Remove horizontal lines
horizontal_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (50, 1))
detect_horizontal = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, horizontal_kernel, iterations=2)
cnts = cv2.findContours(detect_horizontal, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
cv2.drawContours(thresh, [c], -1, (0, 0, 0), 2)
# Remove vertical lines
vertical_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (1, 15))
detect_vertical = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, vertical_kernel, iterations=2)
cnts = cv2.findContours(detect_vertical, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
cv2.drawContours(thresh, [c], -1, (0, 0, 0), 3)
# Dilate to connect text and remove dots
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (10, 1))
dilate = cv2.dilate(thresh, kernel, iterations=2)
cnts = cv2.findContours(dilate, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
area = cv2.contourArea(c)
if area < 500:
cv2.drawContours(dilate, [c], -1, (0, 0, 0), -1)
# Bitwise-and to reconstruct image
result = cv2.bitwise_and(image, image, mask=dilate)
result[dilate == 0] = (255, 255, 255)
# OCR
data = pytesseract.image_to_string(result, lang='mkd+eng', config='--psm 6')
# data = pytesseract.image_to_string(result,config='--psm 6')
#print(data)
worksheet.write(row, 0, data)
row = row + 1
workbook.close()
def sort_contours(cnts, method="left-to-right"):
# initialize the reverse flag and sort index
reverse = False
i = 0
# handle if we need to sort in reverse
if method == "right-to-left" or method == "bottom-to-top":
reverse = True
# handle if we are sorting against the y-coordinate rather than
# the x-coordinate of the bounding box
if method == "top-to-bottom" or method == "bottom-to-top":
i = 1
# construct the list of bounding boxes and sort them from top to
# bottom
boundingBoxes = [cv2.boundingRect(c) for c in cnts]
(cnts, boundingBoxes) = zip(*sorted(zip(cnts, boundingBoxes),
key=lambda b: b[1][i], reverse=reverse))
# return the list of sorted contours and bounding boxes
return (cnts, boundingBoxes)
def get_table(path):
image = cv2.imread(path, 0)
image_colour=cv2.imread(path)
ret, img = cv2.threshold(image, 240, 255, cv2.THRESH_BINARY)
img_inv = 255 - img
kernel_len = np.array(img).shape[1] // 100
# Defining a vertical kernel to detect all vertical lines of image
ver_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (1, kernel_len))
img_bin = img_inv
image_1 = cv2.erode(img_bin, ver_kernel, iterations=3)
vertical_lines = cv2.dilate(image_1, ver_kernel, iterations=3)
cv2.imwrite("vertical.jpg", vertical_lines)
hor_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (kernel_len, 1))
# A kernel of 2x2
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (2, 2))
image_2 = cv2.erode(img_bin, hor_kernel, iterations=3)
horizontal_lines = cv2.dilate(image_2, hor_kernel, iterations=3)
cv2.imwrite("horizontal.jpg", horizontal_lines)
img_vh = cv2.addWeighted(vertical_lines, 0.5, horizontal_lines, 0.5, 0.0)
# Eroding and thesholding the image
img_vh = cv2.erode(~img_vh, kernel, iterations=2)
thresh, img_vh = cv2.threshold(img_vh, 128, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
cv2.imwrite("img_vh.jpg", img_vh)
#bitxor = cv2.bitwise_xor(img, img_vh)
#bitnot = cv2.bitwise_not(bitxor)
contours, hierarchy = cv2.findContours(img_vh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
# Sort all the contours by top to bottom.
contours, boundingBoxes = sort_contours(contours, method="top-to-bottom")
# Creating a list of heights for all detected boxes
heights = [boundingBoxes[i][3] for i in range(len(boundingBoxes))]
# Get mean of heights
mean = np.mean(heights)
box = []
# Get position (x,y), width and height for every contour and show the contour on image
for c in contours:
x, y, w, h = cv2.boundingRect(c)
if (100 <w < 0.8*image.shape[1] and 40 < h < 500):
image = cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)
box.append([x, y, w, h])
return arrange_rows(box,mean),image_colour
def arrange_rows(box,mean):
row = []
column = []
j = 0
# Sorting the boxes to their respective row and column
for i in range(len(box)):
if (i == 0):
column.append(box[i])
previous = box[i]
else:
if (box[i][1] <= previous[1] + mean / 2):
column.append(box[i])
previous = box[i]
if (i == len(box) - 1):
row.append(column)
else:
row.append(column)
column = []
previous = box[i]
column.append(box[i])
return row
def cell_ocr(im,rcts):
rcts = [sorted(c, key=lambda x: x[0]) for c in rcts]
output = []
for i, row in enumerate(rcts):
y, x, w, h = row[0]
y1, x1, w1, h1 = row[1]
finalimg = im[x:x + h, y:y + w]
finalimg_val = im[x1:x1 + h1, y1:y1 + w1]
resizing = cv2.resize(finalimg, None, fx=2, fy=2, interpolation=cv2.INTER_CUBIC)
resizing_val = cv2.resize(finalimg_val, None, fx=2, fy=2, interpolation=cv2.INTER_CUBIC)
out = pytesseract.image_to_string(resizing, lang='mkd+eng')
out_val = pytesseract.image_to_string(resizing_val, lang='mkd+eng')
output.append([out.strip(), out_val.strip()])
return output
def get_text_v(path="images", date_of=(date.today() - timedelta(days=1))):
type_dict = {}
for f in os.listdir(path):
print("Processing File : " + str(f) + " ...")
r, im = get_table(os.path.join(path,f))
output=cell_ocr(im,r)
try:
idx=[x[0] for x in output].index("Вид на упис")
attr_key = output[idx][1]
except ValueError:
attr_key = "custom"
if attr_key in type_dict:
grp_df=pd.DataFrame(output).groupby(0,as_index=False).agg(lambda x: ",".join([str(xc) for xc in x]))
type_dict[attr_key]=type_dict[attr_key].merge(grp_df, how="outer",on=0)
else:
type_dict[attr_key]=pd.DataFrame(output).groupby(0,as_index=False).agg(lambda x: ",".join([str(xc) for xc in x]))
type_dict.pop('Упис на промена', None) # this should delete the Упис на промена sheet
type_dict.pop('Упис на основање', None) # this should delete the Упис на основање sheet
type_dict.pop('Упис на основање', None) # this should delete the Упис на основање sheet
with pd.ExcelWriter("workbook"+str(date_of)+'.xlsx') as writer:
for k, v in type_dict.items():
v.transpose().to_excel(writer, sheet_name=k[:30], header=False, index=False)
return type_dict
def main():
count = 0
driver.get("http://crm.com.mk/mk/otvoreni-podatotsi/objavi-na-upisi-za-subjekti")
time.sleep(30)
for l in range(len(excel_name)):
print("visiting option : " + excel_name[l])
data_list = []
if (l < 1):
continue
today = str(date.today() - timedelta(days=3)).split('-')
get_date = today[2] + '.' + today[1] + '.' + today[0]
driver.find_element_by_xpath(
'//*[#id="content"]/cms-container/crm-template-fs-latestannouncement/crm-cnt-latestannouncement/crm-cnt-latestannouncement-list/div/crm-cnt-latestannouncement-list-oss/div[2]/div/div[1]/div[2]/div[1]/fieldset/span/select/option[' + str(
l + 1) + ']').click()
time.sleep(2)
driver.find_element_by_xpath(
'//*[#id="content"]/cms-container/crm-template-fs-latestannouncement/crm-cnt-latestannouncement/crm-cnt-latestannouncement-list/div/crm-cnt-latestannouncement-list-oss/div[2]/div/div[1]/div[2]/div[2]/fieldset/input').send_keys(
get_date)
time.sleep(2)
driver.find_element_by_xpath(
'//*[#id="content"]/cms-container/crm-template-fs-latestannouncement/crm-cnt-latestannouncement/crm-cnt-latestannouncement-list/div/crm-cnt-latestannouncement-list-oss/div[2]/div/div[2]/div/button[1]').click()
time.sleep(10)
page_content = soup(driver.page_source, 'html.parser')
if (page_content.find('table', {'class': 'table--mobile'}) != None):
if (page_content.find('ul', {'class': 'ngx-pagination'}) != None):
page_list = page_content.find('ul', {'class': 'ngx-pagination'}).findAll("li")
print(page_list[len(page_list) - 2].text.replace('page ', ''))
for i in range(int(page_list[len(page_list) - 2].text.replace('page ', ''))):
time.sleep(3)
driver.find_element_by_xpath(
'//*[#id="content"]/cms-container/crm-template-fs-latestannouncement/crm-cnt-latestannouncement/crm-cnt-latestannouncement-list/div/crm-cnt-latestannouncement-list-oss/div[4]/div/div/pagination-controls/pagination-template/ul/li[' + str(
i + 3) + ']').click()
time.sleep(3)
page_res = soup(driver.page_source, 'html.parser')
if (page_res.find('table', {'class': 'table--mobile'}) != None):
table_list = page_res.find('table', {'class': 'table--mobile'}).findAll('tr')
for j in range(len(table_list)):
if (j > 0):
tr_list = table_list[j].findAll('td')
data_list.append(tr_list[0].text)
else:
count = 1
if count == 1:
break
else:
table_list = page_content.find('table', {'class': 'table--mobile'}).findAll('tr')
for j in range(len(table_list)):
if (j > 0):
tr_list = table_list[j].findAll('td')
data_list.append(tr_list[0].text)
print("number of items found in option " + excel_name[l] + " : " + str(len(data_list)))
data_number = len(data_list)
if (data_number == 0):
driver.find_element_by_xpath(
'//*[#id="content"]/cms-container/crm-template-fs-latestannouncement/crm-cnt-latestannouncement/crm-cnt-latestannouncement-list/div/crm-cnt-latestannouncement-list-oss/div[2]/div/div[1]/div[2]/div[2]/fieldset/input').clear()
continue
for k in range(len(data_list)):
print("Downloading image number : " + str(k) + "/" + str(len(data_list)))
#if(k>2):
# break
driver.get("http://crm.com.mk/mk/otvoreni-podatotsi/objavi-na-upisi-za-subjekti?id=" + data_list[k] + "&p=1")
time.sleep(60)
page_cont = soup(driver.page_source, 'html.parser')
if (page_cont.find('div', {'class': 'row center'}) != None):
image_src = page_cont.find('div', {'class': 'row center'}).div.img['src']
try:
imagedata = base64.b64decode(image_src.replace('data:image/png;base64,', ''))
image = open("./images/" + str(date.today() - timedelta(days=)) + '-' + image_name[l] + str(k) + ".png", "wb")
image.write(imagedata)
image.close()
except:
print("An exception occurred on image " + str(k) +" with id : " + str(data_list[k]) )
driver.get("http://crm.com.mk/mk/otvoreni-podatotsi/objavi-na-upisi-za-subjekti")
time.sleep(20)
if excel_name[l]=="Упис на промена":
get_text(data_number, image_name[l], excel_name[l])
if excel_name[l]=="Упис на основање":
get_text(data_number, image_name[l], excel_name[l])
count = 0
driver.close()
main()
print("Generating workbook please wait ...")
get_text_v()
print("Workbook file generated !!")
print("Moving files from images to oldimages ...")
source_dir = 'images'
target_dir = 'oldimages'
file_names = os.listdir(source_dir)
for file_name in file_names:
print("moving file " + str(file_name) + " ...")
try:
shutil.move(os.path.join(source_dir, file_name), target_dir)
except:
print("An exception occurred, File already exist !!!")
print("Moving files from images to oldimages Done !!!")
I have a custom object detection code written in Python using Yolo. I want to write some output values like the image_name, detected_object_label,box_co-ordinate values, confidence percentage into 2 csv files. 1 csv should have image_name, detected_object_label,confidence percentage and the other should have image_name, detected_object_label,box_co-ordinate values
I have made the code for custom object detection till now.
import cv2
import glob
from glob import glob
import os
import shutil
import numpy as np
import argparse
import imutils
from keras.preprocessing.image import img_to_array
from keras.models import load_model
#import tensorflow as tf
from PIL import Image
import json
import sys
import csv
#from utils import label_map_util
#from utils import visualization_utils as vis_util
from os.path import isfile,join
import pandas as pd
########################################## EXTRACT FRAMES FROM VIDEO###########################
def extractFrames(m):
global vid_name
global img_name
vid_files=glob(m)
complete_videos = get_completed_videos()
new_vid_files = [x for x in vid_files if get_vid_name(x) not in complete_videos]
for vid in new_vid_files:
print("path of video========>>>>.",vid)
v1 = os.path.basename(vid)
try:
vid_name = get_vid_name(vid)
vidcap = cv2.VideoCapture(vid)
except cv2.error as e:
print(e)
except:
print('error')
fsize = os.stat(vid)
print('=============size of video ===================:' , fsize.st_size)
try:
if ( fsize.st_size > 1000 ):
fps = vidcap.get(cv2.CAP_PROP_FPS) # OpenCV2 version 2 used "CV_CAP_PROP_FPS"
frameCount = int(vidcap.get(cv2.CAP_PROP_FRAME_COUNT))
duration = frameCount/fps
minutes = int(duration/60)
print('fps = ' + str(fps))
print('number of frames = ' + str(frameCount))
print('duration (S) = ' + str(duration))
if (duration <= 5):
success,image = vidcap.read()
count=0
success=True
while success:
img_name = vid_name + '_f' + str(count) + ".jpg"
success,image = vidcap.read()
if count % 10 == 0 or count ==0:
target_non_target(img_name, image)
count+=1
vidcap.release()
cv2.destroyAllWindows()
elif (duration > 5 and duration <= 10):
success,image = vidcap.read()
count = 0
success = True
while success:
img_name = vid_name + '_f' + str(count) + ".jpg"
success,image = vidcap.read()
if count%20 == 0 or count == 0:
target_non_target(img_name,image)
count+=1
vidcap.release()
cv2.destroyAllWindows()
elif (duration > 10 and duration <= 20):
success,image = vidcap.read()
count = 0
success = True
while success:
img_name = vid_name + '_f' + str(count) + ".jpg"
success,image = vidcap.read()
if count%30 == 0 or count == 0:
target_non_target(img_name,image)
count+=1
vidcap.release()
cv2.destroyAllWindows()
elif (duration > 20 and duration <= 100):
success,image = vidcap.read()
count = 0
success = True
while success:
img_name = vid_name + '_f' + str(count) + ".jpg"
success,image = vidcap.read()
if count%50 == 0 or count == 0:
target_non_target(img_name,image)
count+=1
vidcap.release()
cv2.destroyAllWindows()
elif (duration > 100 and duration <= 300):
success,image = vidcap.read()
count = 0
success = True
while success:
img_name = vid_name + '_f' + str(count) + ".jpg"
success,image = vidcap.read()
if count%100 == 0 or count == 0:
target_non_target(img_name,image)
count+=1
vidcap.release()
cv2.destroyAllWindows()
elif (duration > 300 and duration <= 600):
success,image = vidcap.read()
count = 0
success = True
while success:
img_name = vid_name + '_f' + str(count) + ".jpg"
success,image = vidcap.read()
if count%150 == 0 or count == 0:
target_non_target(img_name,image)
count+=1
vidcap.release()
cv2.destroyAllWindows()
elif (duration > 600 and duration <= 900):
success,image = vidcap.read()
count = 0
success = True
while success:
img_name = vid_name + '_f' + str(count) + ".jpg"
success,image = vidcap.read()
if count%300 == 0 or count == 0:
target_non_target(img_name,image)
count+=1
vidcap.release()
cv2.destroyAllWindows()
elif (duration > 900 and duration <= 1200):
success,image = vidcap.read()
count = 0
success = True
while success:
img_name = vid_name + '_f' + str(count) + ".jpg"
success,image = vidcap.read()
if count%600 == 0 or count == 0:
target_non_target(img_name,image)
count+=1
vidcap.release()
cv2.destroyAllWindows()
elif (duration > 1200 and duration <= 1800):
success,image = vidcap.read()
count = 0
success = True
while success:
img_name = vid_name + '_f' + str(count) + ".jpg"
success,image = vidcap.read()
if count%900 == 0 or count == 0:
target_non_target(img_name,image)
count+=1
vidcap.release()
cv2.destroyAllWindows()
elif (duration > 1800 and duration <= 3600):
success,image = vidcap.read()
count = 0
success = True
while success:
img_name = vid_name + '_f' + str(count) + ".jpg"
success,image = vidcap.read()
if count%1200 == 0 or count == 0:
target_non_target(img_name,image)
count+=1
vidcap.release()
cv2.destroyAllWindows()
elif (duration > 3600 and duration <= 7200):
success,image = vidcap.read()
count = 0
success = True
while success:
img_name = vid_name + '_f' + str(count) + ".jpg"
success,image = vidcap.read()
if count%1500 == 0 or count == 0:
target_non_target(img_name,image)
count+=1
vidcap.release()
cv2.destroyAllWindows()
elif (duration > 7200 and duration <= 10800):
success,image = vidcap.read()
count = 0
success = True
while success:
img_name = vid_name + '_f' + str(count) + ".jpg"
success,image = vidcap.read()
if count%2000 == 0 or count == 0:
target_non_target(img_name,image)
count+=1
vidcap.release()
cv2.destroyAllWindows()
elif (duration > 10800):
success,image = vidcap.read()
count = 0
success = True
while success:
img_name = vid_name + '_f' + str(count) + ".jpg"
success,image = vidcap.read()
if count%2500 == 0 or count == 0:
target_non_target(img_name,image)
count+=1
vidcap.release()
cv2.destroyAllWindows()
else:
print("video length exceeds max limit")
except:
print("error")
print('finished processing video ', vid)
with open('C:\\Python35\\target_non_target\\'+'video_info.csv', 'a') as csv_file:
fieldnames = ['Video_Name','Process']
file_is_empty = os.stat('C:\\Python35\\target_non_target\\'+'video_info.csv').st_size == 0
writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
if file_is_empty:
writer.writeheader()
writer.writerow({'Video_Name':vid_name,'Process':'done'})
#print('finished processing video {0} with frames {1}'.format(vid_files[v_f], count))
def get_vid_name(vid):
return os.path.splitext(os.path.basename(vid))[0]
def get_completed_videos():
completed_videos = []
with open("C:\\Python35\\target_non_target\\video_info.csv") as csv_file:
for row in csv.reader(csv_file):
for col in range(0,len(row)):
try:
completed_videos.append(row[col])
except Exception as e:
print(str(e))
print(completed_videos[0])
return completed_videos
###########################CHECK FOR TARGET IMAGES FROM FRAMES###########################
def target_non_target(img_name, image):
try:
image_re = cv2.resize(image, (28, 28))
image_fl = image_re.astype("float") / 255.0
image_arr = img_to_array(image_fl)
image_expanded = np.expand_dims(image_arr, axis=0)
model_file = "C:\\Python35\\target_non_target\\target_non_target.model"
model = load_model(model_file)
(non_target, target) = model.predict(image_expanded)[0]
if target > non_target:
print("[INFO] Target DETECTED...")
#recognize_object(image)
draw_pred(image, class_ids[i], confidences[i], round(x), round(y), round(x + w), round(y + h))
else:
print("[INFO] NON TARGET...")
except Exception as e:
print(str(e))
#################################OBJECT RECOGNITION MODEL CODE################################
#def recognize_object(image):
yolo_cfg = "C:\\Python35\\custom_yolo_object_detector\\darknet\\custom\\yolov3-tiny.cfg"
weights = "C:\\Python35\\custom_yolo_object_detector\\darknet\\custom\\yolov3-tiny_final.weights"
class_names = "C:\\Python35\\custom_yolo_object_detector\\darknet\\custom\\objects.names"
def getOutputsNames(net):
layersNames = net.getLayerNames()
return [layersNames[i[0] - 1] for i in net.getUnconnectedOutLayers()]
def draw_pred(image, class_id, confidence, x, y, x_plus_w, y_plus_h):
label = str(classes[class_id])
color = COLORS[class_id]
cv2.rectangle(img, (x,y), (x_plus_w,y_plus_h), color, 2)
cv2.putText(img, label, (x-10,y-10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
classes = None
with open(class_names, 'r') as f:
classes = [line.strip() for line in f.readlines()]
print(classes)
COLORS = np.random.uniform(0, 255, size=(len(classes), 3))
net = cv2.dnn.readNet(weights,yolo_cfg)
image=cv2.resize(image, (299, 299))
blob = cv2.dnn.blobFromImage(image, 1.0/255.0, (299,299), [0,0,0], True, crop=False)
Width = image.shape[1]
Height = image.shape[0]
net.setInput(blob)
outs = net.forward(getOutputsNames(net))
class_ids = []
confidences = []
boxes = []
conf_threshold = 0.5
nms_threshold = 0.4
for out in outs:
for detection in out:
scores = detection[5:]
class_id = np.argmax(scores)
confidence = scores[class_id]
if confidence > 0.5:
center_x = int(detection[0] * Width)
center_y = int(detection[1] * Height)
w = int(detection[2] * Width)
h = int(detection[3] * Height)
x = center_x - w / 2
y = center_y - h / 2
class_ids.append(class_id)
confidences.append(float(confidence))
boxes.append([x, y, w, h])
indices = cv2.dnn.NMSBoxes(boxes, confidences, conf_threshold, nms_threshold)
for i in indices:
i = i[0]
box = boxes[i]
x = box[0]
y = box[1]
w = box[2]
h = box[3]
#draw_pred(obj_img, class_ids[i], confidences[i], round(x), round(y), round(x+w), round(y+h))
cv2.putText(image, label, (0, 15), cv2.FONT_HERSHEY_SIMPLEX, .6, (255, 0, 0))
cv2.imshow(image)
if __name__ == "__main__":
x="C:\\Python36\\videos\\*.mp4"
extractFrames(x)
You could pass in your values to this function
def writeToCSV(value1,value2,value3...):
with open('new.csv', 'a') as writeFile:
writer = csv.writer(writeFile)
for i in range(len(result)):
writer.writerow(result)
I've a problem with my software in Python. It's a big while cicle where I took a intel realsense (USB camera) stream. Using opencv I make a couple of findContours and I send the results of contours to another software.
The problem is that there is a memory consuption. In fact the RAM usage increase every 2-3 seconds by 0.1%.
II don't know what to do...
This is the code (sorry if it's not beautifull but I'm testing a lot of things)
import numpy as np
import random
import socket
import cv2
import time
import math
import pickle
import httplib, urllib
from xml.etree import ElementTree as ET
import logging
logging.basicConfig(level=logging.INFO)
try:
import pyrealsense as pyrs
except:
print("No pyralsense Module installed!")
#funzione per registrare gli eventi del mouse
def drawArea(event,x,y, flag, param):
global fx,fy,ix,iy
if event == cv2.EVENT_LBUTTONDOWN:
ix,iy = x,y
elif event == cv2.EVENT_LBUTTONUP:
fx,fy = x,y
def RepresentsInt(s):
try:
int(s)
return True
except ValueError:
return False
quit = False
read = False
while read == False:
file = open('default.xml', 'r')
tree = ET.parse(file)
root = tree.getroot()
for child in root:
if child.tag == "intel":
intel = int(child[0].text)
elif child.tag == "output":
portOut = int(child[2].text)
elif child.tag =="source":
video_source = child.text
file.close()
root.clear()
ix,iy = -1,-1
fx,fy = -1,-1
timeNP = 10
last = time.time()
smoothing = 0.9
fps_smooth = 30
#video_source = video_source.split(",")
read = True
if RepresentsInt(video_source):
video_source = int(video_source)
if intel == 1:
pyrs.start()
dev = pyrs.Device(video_source)
master = 1
address = ('', 3333)
broadSockListe = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
broadSockListe.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
broadSockListe.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
broadSockListe.bind(('',3333))
while True:
if master == 0:
datas, address = broadSockListe.recvfrom(1024)
if str(datas) == "8000":
separator = ":"
seq = (address[0],"8081")
masterAddr = separator.join(seq)
IP = str([l for l in (
[ip for ip in socket.gethostbyname_ex(socket.gethostname())[2] if not ip.startswith("127.")][:1], [
[(s.connect(('8.8.8.8', 53)), s.getsockname()[0], s.close()) for s in
[socket.socket(socket.AF_INET, socket.SOCK_DGRAM)]][0][1]]) if l][0][0])
params = separator.join(("addUnit",IP,str(portOut),"camera","generalList.xml"))
params = urllib.urlencode({"Python":params})
headers = {}
conn = httplib.HTTPConnection(masterAddr)
conn.request("POST",masterAddr ,params, headers)
params = separator.join(("masterIP",address[0],str(portOut)+"/","default.xml"))
params = urllib.urlencode({"Python":params})
headers = {}
myip = IP + ":8081"
conn = httplib.HTTPConnection(myip)
#eseguo una post al mio server
conn.request("POST", myip, params, headers)
broadSockListe.close()
#imposto master a 1 per dire che l'ho registrato e posso partire col programma
master = 1
read = False
while read == False:
'''# leggo le varie impostazioni dal file default
file = open('default.xml','r+')
tree = ET.parse(file)
root = tree.getroot()
for child in root:
if child.tag == "modifica" and child.text == "1":
child.text = "0"
tree.write('default.xml')
root.clear()
file.close()'''
read = True
prev,prevprev,dirX,dirY = 0,0,0,0
spostamento = 15
UDP_IP = ["", ""]
UDP_PORT = ["", ""]
UDP_IP[0] = "127.0.0.1"
UDP_PORT[0] = 3030
IP_left = "127.0.0.1"
IP_right = "127.0.0.1"
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind(("",portOut))
message = ""
sep = "-"
font = cv2.FONT_HERSHEY_SIMPLEX
kernel = cv2.getStructuringElement(cv2.MORPH_CROSS, (3, 3))
#rettangoli = [x,y,width,height,angle,box, area, contours]
rettangoli = []
cnt = 0
letto = 0
while True:
now = time.time()
if letto < now - 2 or letto == 0 or now < letto:
letto = now
print(now)
read = False
while read == False:
file = open('default.xml', 'r')
tree = ET.parse(file)
root = tree.getroot()
for child in root:
if child.tag == "output":
UDP_IP[1] = child[0].text
UDP_PORT[1] = int(child[1].text)
if child.tag == "effects":
erode = int(child[0].text)
erodePos = int(child[1].text)
erode2 = int(child[2].text)
erodePos2 = int(child[3].text)
dilate1 = int(child[4].text)
dilatePos1= int(child[5].text)
dilate2 = int(child[6].text)
dilatePos2 = int(child[7].text)
blur = int(child[8].text)
blurPos = int(child[9].text)
if child.tag == "intel":
val1Min = int(child[1].text)
val1Max = int(child[2].text)
val2Min = int(child[3].text)
val2Max = int(child[4].text)
val3Min = int(child[5].text)
val3Max = int(child[6].text)
if child.tag == "modifica":
if child.text == "1":
break
#definisco dimensioni per collisioni
if child.tag == "size":
blobSize= int(child[0].text)
dimBordoBlob= int(child[1].text)
if child.tag == "visualizza":
visualizza= child.text
if child.tag == "feedback":
SFB = int(child.text)
root.clear()
file.close()
read = True
dev.wait_for_frame()
c = dev.colour
c = cv2.cvtColor(c, cv2.COLOR_RGB2BGR)
d = dev.depth * dev.depth_scale * -60
d = d[5:485, 25:635]
d = cv2.applyColorMap(d.astype(np.uint8), cv2.COLORMAP_HSV)
c = cv2.resize(c, (320 ,240), interpolation=cv2.INTER_AREA)
d = cv2.resize(d, (320,240), interpolation=cv2.INTER_AREA)
#trasformo i colori in HSV per filtrarli
frame = cv2.cvtColor(d, cv2.COLOR_BGR2HSV)
lower_red = np.array([val1Min, val2Min, val3Min])
upper_red = np.array([val1Max, val2Max, val3Max])
frame = cv2.inRange(frame, lower_red, upper_red)
dimensions = frame.shape
widthStream = dimensions[1]
heightStream = dimensions[0]
roomFrame = np.zeros(( heightStream,widthStream, 3), np.uint8)
roomFrame[:] = (0, 0, 0)
fgmask = frame
halfheight = int(heightStream / 2)
halfwidth = int(widthStream / 2)
for i in range(0, 15):
if erode >= 1 and erodePos == i:
fgmask = cv2.erode(fgmask, kernel, iterations=erode)
if dilate1 >= 1 and dilatePos1 == i:
fgmask = cv2.dilate(fgmask, kernel, iterations=dilate1)
if erode2 >= 1 and erodePos2 == i:
fgmask = cv2.erode(fgmask, kernel, iterations=erode2)
if dilate2 >= 1 and dilatePos2 == i:
fgmask = cv2.dilate(fgmask, kernel, iterations=dilate2)
if blur == 1 and blurPos == 1:
fgmask = cv2.GaussianBlur(fgmask, (5, 5), 0)
if ix > fx:
temp = fx
fx = ix
ix = temp
if iy > fy:
temp = fy
fy = iy
iy = temp
if cnt == 0:
ix,iy = 1,1
fx,fy = widthStream-1,heightStream-1
fgmask, contours, hierarchy = cv2.findContours(fgmask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
rettangoli = []
for cont in contours:
rect = cv2.minAreaRect(cont)
box = cv2.boxPoints(rect)
box = np.int0(box)
width = rect[1][0]
height = rect[1][1]
angle = rect[2]
if width > height:
angle = 180 + angle
else:
angle = 270 + angle
x, y, w, h = cv2.boundingRect(cont)
centerX = int(w / 2 + x)
centerY = int(h / 2 + y)
M = cv2.moments(cont)
area = int(M['m00'])
if area > blobSize:
if ix < centerX < fx and iy < centerY < fy:
cv2.drawContours(fgmask, [cont], 0, (100, 100, 100), dimBordoBlob)
cv2.drawContours(fgmask, [cont], 0, (255, 255, 255), -1)
rettangoli.append([centerX, centerY, w, h, angle, box, area, cont])
indice = 0
fgmask, contours, hierarchy = cv2.findContours(fgmask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_TC89_KCOS)
if intel == 1:
fgmask = cv2.cvtColor(fgmask, cv2.COLOR_GRAY2RGB)
rettangoli = []
for cont in contours:
rect = cv2.minAreaRect(cont)
box = cv2.boxPoints(rect)
box = np.int0(box)
width = rect[1][0]
height = rect[1][1]
angle = rect[2]
if width > height:
angle = 180 + angle
else:
angle = 270 + angle
x, y, w, h = cv2.boundingRect(cont)
centerX = int(w / 2 + x)
centerY = int(h / 2 + y)
M = cv2.moments(cont)
indice += 1
if M['m00'] > blobSize:
if ix < centerX < fx and iy < centerY < fy:
rettangoli.append([centerX, centerY, w, h, angle, box, int(M['m00']), cont])
cv2.drawContours(roomFrame, [cont], 0, (255, 255, 255), -1)
for rett in rettangoli:
seq = (message,np.array_str(rett[7]))
message = sep.join(seq)
temp = 0
while temp < len(UDP_IP):
sock.sendto(bytes(message), (UDP_IP[temp], UDP_PORT[temp]))
temp += 1
message = ""
if SFB == 1:
cv2.imshow("Camera Intel", roomFrame)
if cv2.waitKey(1) & 0xFF == ord('r'):
break
if cv2.waitKey(1) & 0xFF == ord('q'):
quit = True
break
name = "color.jpeg"
cv2.imwrite(name, c)
name = "bn.jpeg"
cv2.imwrite(name, roomFrame)
if intel == 0:
cap.release()
cv2.destroyAllWindows()
You are creating new objects in your while loop. Take now for example, you create a variable and then you assign a new object to it that only lives in that loop. If you declare the variables before your loop the same object will be overwritten instead of re-created.
By just declaring the variables ahead of time with name = None you will be able to make sure you reuse these variables.
I hope this works for you.