Return multiple values to script - python

In the script below I import some pictures that I want to segmentate. The segmentation is done withe the line:
mask = cv.inRange(blur0, low_yellow, high_yellow)
As you can see, normally the low_yellow and high_yellow is given. But depending the color of the pictures, I need a different segmentation. So, I created a listbox in Tkinter with the different colors. When I select some item in the listbox, I want to make a return value who fills in the low_yellow and the high_yellow. So 2 different return values. I did already some trail and error, but couldn't find the solution. My question is, is it possible to make 2 different renturn values and hwo?
from tkinter import *
from tkinter import filedialog
import tkinter as tk
import datetime
import cv2 as cv
import glob
import numpy as np
import pandas as pd
from colormath.color_objects import sRGBColor, xyYColor, LabColor, XYZColor
from colormath.color_conversions import convert_color
import os
# create folder for subfolders
Foldername = 'Kleurmeting_output'
mainfolder = os.getcwd() + '\\' + Foldername
if not os.path.exists(mainfolder):
os.makedirs(mainfolder)
def Innovator(ImagePath, SavePath, LowY, HighY):
dfs = []
for file in glob.glob(ImagePath):
print(file)
img = cv.imread(file)
scale_percent = 60
width = int(img.shape[1] * scale_percent / 100)
height = int(img.shape[0] * scale_percent / 100)
dim = (width, height)
imgr = cv.resize(img, dim, interpolation=cv.INTER_AREA)
hsv = cv.cvtColor(imgr, cv.COLOR_BGR2HSV)
blur0 = cv.medianBlur(hsv, 11)
#low_yellow = np.array([10, 42, 210])
#high_yellow = np.array([30, 255, 255])
low_yellow = LowY
high_yellow = HighY
print(low_yellow)
print(high_yellow)
mask = cv.inRange(blur0, low_yellow, high_yellow)
res = cv.bitwise_and(imgr, imgr, mask=mask)
fname = os.path.splitext(os.path.basename(file))[0]
# print(fname)
Imagefolder = str(SavePath) + '\\' + 'Gesegmenteerde afbeelding'
if not os.path.exists(Imagefolder):
os.makedirs(Imagefolder)
cv.imwrite(str(SavePath) + f'/Gesegmenteerde afbeelding/{fname}.jpg', res)
result_df = pd.DataFrame()
#FileNames = ['Mean']
def run_command():
if Most_Recent == 0: # Geen selectie
print("Select a folder")
elif Most_Recent == 'Image': # Afbeelding
if Listb.get(ANCHOR) == '':
print("Select the potato type")
else:
# Creates subfolder
d = datetime.datetime.now()
SaveFolder = os.getcwd() + '\\' + Foldername + '\\' + str(d.date()) + '_Change_name_later1'
else:
# Folder
if Listb.get(ANCHOR) == '':
print("Select the potato type")
else:
# Creates subfolder
d = datetime.datetime.now()
SaveFolder = os.getcwd() + '\\' + Foldername + '\\' + str(d.date()) + '_Change_name_later'
if not os.path.exists(SaveFolder):
os.makedirs(SaveFolder)
#SavedImage = SaveFolder + '\\' + 'Gesegmenteerde afbeelding' + '*.jpg'
ScriptPath = New_Method_Script_Parser((Listb.get(ANCHOR)))
print(ScriptPath)
Innovator(ImagePath= FolderPath, SavePath= SaveFolder, LowY=ScriptPath, HighY=ScriptPath)
def New_Method_Script_Parser(ListValue):
if ListValue == 'Wit':
return LowY(10, 40, 220), High(30, 255, 255)
elif ListValue == 'Licht geel':
return "--LowY 10 42 210 --HighY 30 255 255"
elif ListValue == 'Geel':
return "--LowY 10 42 200 --HighY 30 255 255"
elif ListValue == 'Donker geel':
return "--LowY 10 42 190 --HighY 30 255 255"
Listb = Listbox(root)
Listb.insert(0, "Wit")
Listb.insert(1, "Licht geel")
Listb.insert(2, "Geel")
Listb.insert(3, "Donker geel")
Listb.place(x=100, y=100)

Related

TensorFlow/Keras multi-threaded model prediction

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.

Socket Programming Python Server and C client

this is what i make in python server code (receiving data length from C client and then receiving image file(.jpg))
but problem is when i receiving data length error occurs "invaild base64-encoded string"
here is my servercode (Removed unnecessary functions) and when trying to run the stringData part the error occurs
import os
import socket
import cv2
import numpy
import base64
import glob
import sys
import time
import threading
from datetime import datetime
class ServerSocket:
def receiveImages(self):
cnt_str = ''
cnt = 0
try:
while True:
if (cnt < 10):
cnt_str = '000' + str(cnt)
elif (cnt < 100):
cnt_str = '00' + str(cnt)
elif (cnt < 1000):
cnt_str = '0' + str(cnt)
else:
cnt_str = str(cnt)
if cnt == 0: startTime = time.localtime()
cnt += 1
length = self.conn.recv(64)
print(length)
length1 = length.decode('utf-8')
print(length1)
stringData = self.recvall(self.conn, int(length1))
## stringData = self.conn.recv(100000)
##print(stringData)
msg = "ready"
self.conn.sendall(msg.encode())
stime = self.recvall(self.conn, 64)
print('send time: ' + stime.decode('utf-8'))
now = time.localtime()
print('receive time: ' + datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S.%f'))
data = numpy.frombuffer(base64.b64decode(stringData), numpy.uint8)
decimg = cv2.imdecode(data, 1)
cv2.imshow("image", decimg)
cv2.imwrite('./' + str(self.TCP_PORT) + '_images' + str(self.folder_num) + '/img' + cnt_str + '.jpg', decimg)
cv2.waitKey(1)
if (cnt == 60 * 10):
cnt = 0
convertThread = threading.Thread(target=self.convertImage(str(self.folder_num), 600, startTime))
convertThread.start()
self.folder_num = (self.folder_num + 1) % 2
except Exception as e:
print(e)
#self.convertImage(str(self.folder_num), cnt, startTime)
self.socketClose()
cv2.destroyAllWindows()
self.socketOpen()
self.receiveThread = threading.Thread(target=self.receiveImages)
self.receiveThread.start()
def recvall(self, sock, count):
buf = b''
while count:
newbuf = sock.recv(count)
if not newbuf: return None
buf += newbuf
count -= len(newbuf)
return buf
def convertImage(self, fnum, count, now):
img_array = []
cnt = 0
for filename in glob.glob('./' + str(self.TCP_PORT) + '_images' + fnum + '/*.jpg'):
if (cnt == count):
break
cnt = cnt + 1
img = cv2.imread(filename)
height, width, layers = img.shape
size = (width, height)
img_array.append(img)
file_date = self.getDate(now)
file_time = self.getTime(now)
name = 'video(' + file_date + ' ' + file_time + ').mp4'
file_path = './videos/' + name
out = cv2.VideoWriter(file_path, cv2.VideqqoWriter_fourcc(*'.mp4'), 20, size)
for i in range(len(img_array)):
out.write(img_array[i])
out.release()
print(u'complete')
def main():
server = ServerSocket('ServerIP', PORT)
if __name__ == "__main__":
main()
what could it be the problems? in my expectation when decode the string data it can be the problem that C client encode causes problem

Getting keyError in my code. I am written a code but in this code i am getting keyerror:1

Path given to the directory
raw = './data/complete_ms_data/'
train_raw_dir = './data/train/'
test_raw_dir = './data/test/'
Number of raw train images
num_train = 20
Now there is problem in given fuction:
def makedir(new_dir):
if not os.path.exists(new_dir):
os.makedirs(new_dir)
if __name__ == '__main__':
if not os.path.exists(train_raw_dir):
makedir(train_raw_dir)
if not os.path.exists(test_raw_dir):
makedir(test_raw_dir)
print('begin processing...')
i = 0
roots = dict()
for root, dirs, files in os.walk(raw):
roots[i] = root
i += 1
del roots[0]
for i in range(0, 64):
if i%2:
del roots[i]
roots = list(roots.values()) # dict --> list
# random.Random(487).shuffle(roots)
img = np.zeros([31, 512, 512]) # 31
for i, root in enumerate(sorted(roots)):
# print(i, root)
png_list = sorted(glob.glob(root+'/*.png'))
# print(png_list)
for j, path in enumerate(png_list):
img[j, :, :] = cv2.imread(path, 0)
img = img.astype(np.uint8)
if i < num_train:
tiff.imsave(train_raw_dir + str(i) + '.tif', img)
else:
tiff.imsave(test_raw_dir + str(i - 20) + '.tif', img)
scio.savemat(test_raw_dir + str(i - 20) + '.mat', {'img':img})
print('Done')
I am written this code for training and testing for the data. I want to train a images
I am getting error in line 49:
del roots[i]
KeyError: 1
Where I am doing wrong?

How to add (merge) multiple images in one directory

I have two types (A and B) of images in one directory and I want to add them together (not concatenate) them like this:
A1.jpeg + B1.jpeg = Merged1.jpeg
A2.jpeg + B2.jpeg = Merged2.jpeg
      ...
AN.jpeg + BN.jpeg = MergedN.jpeg
I don't know how to customize my code so it would work for the whole directory:
import cv2
import os
for i,filenames in os.walk('.'):
A1 = cv2.imread('A1.jpeg',0)
B1 = cv2.imread('B1.jpeg',0)
image = cv2.add([A1,B1])
filename = ('Merged' + {i} + '.jpeg')
cv2.imwrite(filename, image)
Any ideas? Thanks
EDIT:
I added counter in for loop, because you cannot define for loop in the way I did before.
import cv2
import os
i=0
for filenames in os.walk('.'):
i = i + 1
A = "A" + str(i) + ".jpeg"
B = "B" + str(i) + ".jpeg"
Ai = cv2.imread(A,0)
Bi = cv2.imread(B,0)
image = cv2.add([Ai,Bi])
filename = ('Merged' + str(i) + '.jpeg')
cv2.imwrite(filename, image)
But it only adds A1 and B1. Is this a wrong way to count in for loop?
Just change the filename in imread with a custom variable which you can use i in.
A = "A" + str(i) + ".jpeg"
B = "B" + str(i) + ".jpeg"
Ai = cv2.imread(A,0)
Bi = cv2.imread(B,0)
I assumed that i is a number.
You can use glob for that it will be easier i think
import glob
for i, a_file in enumerate(glob.glob('./A*')):
A = cv2.imread(a_file,0)
B = cv2.imread(a_file.replace('A', 'B'),0)
image = cv2.add([A,B])
filename = ('Merged' + {i} + '.jpeg')
cv2.imwrite(filename, image)
I solved it like this:
import glob
import cv2
number_of_images = 750
for a_file in glob.glob('./A*'):
for i in range(1,number_of_images):
A = "A" + str(i) + ".jpeg"
B = "B" + str(i) + ".jpeg"
A1 = cv2.imread(A,0)
B1 = cv2.imread(B,0)
image = cv2.add([A1,B1])
filename = ('Merged' + str(i) + '.jpeg')
cv2.imwrite(filename, image)
print(i)
if i==number_of_images-1:
break
Thanks for your advices!

Python PIL - Resizing Images Without Keeping Aspect Ratio

So I've got a Python script that takes a bunch of images in a folder, and puts them together into arrays (like this). I also have another script that takes the frames of a video and puts them together in arrays. The problem is, the one that takes the frames from a video creates black bars between the images.
Here is the correct image made using the first script, which uses JPEGS:
Here is the wrong image made using the second script, which uses video frames:
Here is the script that makes the correct first image:
import Image
import glob
import os
name = raw_input('What is the file name (excluding the extension) of your video that was converted using FreeVideoToJPGConverter?\n')
x_res = int(raw_input('What do you want the width of your image to be (in pixels)?\n'))
y_res = int(raw_input('What do you want the height of your image to be (in pixels)?\n'))
rows = int(raw_input('How many rows do you want?\n'))
columns = int(raw_input('How many columns do you want?\n'))
images = glob.glob('./' + name + ' (*)/' + name + '*.jpg')
new_im = Image.new('RGB', (x_res,y_res))
x_cntr = 0
y_cntr = 0
if not os.path.exists('./' + name + ' Output/'):
os.makedirs('./' + name + ' Output/')
for x in xrange(0,len(images),1):
if x%(rows*columns) == 0:
new_im.save('./' + name + ' Output/' + str(x) + '.jpg')
new_im = Image.new('RGB', (x_res,y_res))
y_cntr = 0
x_cntr = 0
print str(round(100*(float(x)/len(images)), 1)) + "% Complete"
elif x%rows == 0:
x_cntr = 0
y_cntr = y_cntr + y_res/columns
elif x%1 == 0:
x_cntr = x_cntr + x_res/rows
im = Image.open(images[x])
im = im.resize((x_res/rows + x_res%rows, y_res/columns + y_res%columns), Image.ANTIALIAS)
new_im.paste(im, (x_cntr, y_cntr))
Here is the script that makes the incorrect second image:
import cv2, Image, os
name = raw_input('Video File (With Extension): ')
x_res = int(raw_input('Image Width (Pixels): '))
y_res = int(raw_input('Image Height (Pixels): '))
rows = int(raw_input('Number of Rows: '))
columns = int(raw_input('Number of Columns: '))
vidcap = cv2.VideoCapture(name)
success,im = vidcap.read()
frames = int(vidcap.get(cv2.CAP_PROP_FRAME_COUNT))
new_im = Image.new('RGB', (x_res, y_res))
x_cntr = 0
y_cntr = 0
print str(frames) + " Frames to Join"
if not os.path.exists('./' + name + ' Output/'):
os.makedirs('./' + name + ' Output/')
for x in xrange(0,frames,1):
if x%(rows*columns) == 0:
new_im.save('./' + name + ' Output/' + str(x) + '.jpg')
new_im = Image.new('RGB', (x_res,y_res))
y_cntr = 0
x_cntr = 0
print str(round(100*(float(x)/frames), 1)) + "% Complete"
elif x%rows == 0:
x_cntr = 0
y_cntr = y_cntr + y_res/columns
elif x%1 == 0:
x_cntr = x_cntr + x_res/rows
success,cv2_im = vidcap.read()
if success == True:
cv2_im = cv2.cvtColor(cv2_im,cv2.COLOR_BGR2RGB)
im = Image.fromarray(cv2_im)
im = im.resize((x_res/rows + x_res%rows, y_res/columns + y_res%columns), Image.ANTIALIAS)
new_im.paste(im, (x_cntr, y_cntr))
elif success == False:
new_im.save('./' + name + ' Output/' + str(x) + '.jpg')
print str(round(100*(float(x)/frames), 1)) + "% Complete" #Why isn't this 100%, fix
As you can see, this specific line for resizing the image (to fit the new array of images) is exactly the same in both scripts:
im = im.resize((x_res/rows + x_res%rows, y_res/columns + y_res%columns), Image.ANTIALIAS)
...Except in the first script, the image is opened from a JPEG, and in the second script, the image is taken from a video frame using OpenCV2. If I try this with a different video, the same thing happens. It resizes as if I were using .thumbnail instead of .resize.
So why is there a different output even though they are the same script?
PS: I also don't know why there are more output images on the jpeg script than the video script, but that may be the fault of FreeVideoToJPGConverter (a software); I'm not sure though.

Categories

Resources