Good day everyone I’m sorry I’m new to python programming sorry if I’m asking this even this is basic or not. Someone can help me with this? The problem is I want to put the data that has been read by my Pyserial from my Arduino temperature sensor but I don’t know how.
Here code for the Temperature to pyserial:
def tempe():
import serial
import time
ser = serial.Serial('COM5', 9600)
time.sleep(2)
data =[] # empty list to store the data
for i in range(50):
b = ser.readline() # read a byte string
string = b.rstrip() # remove n and r
temp = string <= this data here I want to show to my opencv
data.append(string) # add to the end of data list
time.sleep(0.1) # wait (sleep) 0.1 seconds
ser.close()
And here’s the whole code that I want to show in my PutText on opencv:
def offrecog():
screen2.destroy() <=dont mind this
screen.destroy() <= dont mind this
def recog2(img, classifier, scaleFactor,miNeighbors, color, text, clf):
image = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
features = classifier.detectMultiScale(image, scaleFactor, miNeighbors)
for (x,y,w,h) in features:
cv2.rectangle(img, (x,y),(x+w,y+h), color, 2)
id, pred = clf.predict(image [y:y+h, x:x+w])
confidence = int(100*(1-pred/300))
databases = mysql.connector.connect(
host ="localhost",
user = "userdata",
password = "",
database = "facerecog"
)
mycursor = databases.cursor()
mycursor.execute("SELECT names FROM record WHERE ids= " + str(id))
datas = mycursor.fetchone()
datas = "+".join(datas)
cursor2 = databases.cursor()
cursor2.execute("SELECT course_year FROM record WHERE ids= " + str(id))
datas1 = mycursor.fetchone()
datas1 = "+".join(datas1)
cursor3 = databases.cursor()
cursor3.execute("SELECT positions FROM record WHERE ids= " + str(id))
datas2 = mycursor.fetchone()
datas2 = "+".join(datas2)
if confidence>70:
cv2.putText(img, datas, (x,y+205), cv2.FONT_HERSHEY_SIMPLEX, 0.8, color, 2, cv2.LINE_AA)
cv2.putText(img, datas1, (x,y+230), cv2.FONT_HERSHEY_SIMPLEX, 0.8, color, 2, cv2.LINE_AA)
cv2.putText(img, datas2, (x,y+250), cv2.FONT_HERSHEY_SIMPLEX, 0.8, color, 2, cv2.LINE_AA)
cv2.putText(img, tempe, (x,y+280), cv2.FONT_HERSHEY_SIMPLEX, 0.8, color, 2, cv2.LINE_AA)
markattend(datas,datas1,datas2)
else:
cv2.putText(img, "UNKNOWN", (x,y+205), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0,0,255), 1, cv2.LINE_AA)
return img
faceCascade = cv2.CascadeClassifier("C:\Users\So_Low\Desktop\final_recog\haarcascade_frontalface_default.xml")
clf = cv2.face.LBPHFaceRecognizer_create()
clf.read("trained.xml")
video_capture = cv2.VideoCapture(0)
while True:
ret, img = video_capture.read()
img = recog2(img, faceCascade, 1.3, 4, (255,255,255), "Face", clf)
cv2.imshow("FACE RECOGNITION", img)
if cv2.waitKey(1) & 0xFF == ord('!'):
break
video_capture.release()
cv2.destroyAllWindows()
screen2.destroy()
and I got this error when I run it:
File "c:UsersSo_LowDesktopOffrecogoffrecog.py", line 97, in recog2
cv2.putText(img, wew, (x,y+280), cv2.FONT_HERSHEY_SIMPLEX, 0.8, color, 2, cv2.LINE_AA)
cv2.error: OpenCV(4.5.3) :-1: error: (-5:Bad argument) in function 'putText'
> Overload resolution failed:
> - Can't convert object of type 'function' to 'str' for 'text'
> - Can't convert object of type 'function' to 'str' for 'text'
[ WARN:0] global C:UsersrunneradminAppDataLocalTemppip-req-build-u4kjpz2zopencvmodulesvideoiosrccap_msmf.cpp (438) `anonymous-namespace'::SourceReaderCB::~SourceReaderCB terminating async callback
[Done] exited with code=0 in 14.22 seconds
[Running] python -u "c:UsersSo_LowDesktopOffrecogoffrecog.py"
Exception in Tkinter callback
Traceback (most recent call last):
File "I:Pythonlibtkinter__init__.py", line 1883, in __call__
return self.func(*args)
File "c:UsersSo_LowDesktopOffrecogoffrecog.py", line 136, in login_verify
offrecog()
File "c:UsersSo_LowDesktopOffrecogoffrecog.py", line 113, in offrecog
img = recog2(img, faceCascade, 1.3, 4, (255,255,255), "Face", clf)
File "c:UsersSo_LowDesktopOffrecogoffrecog.py", line 95, in recog2
cv2.putText(img, temps, (x,y+280), cv2.FONT_HERSHEY_SIMPLEX, 0.8, color, 2, cv2.LINE_AA)
cv2.error: OpenCV(4.5.3) :-1: error: (-5:Bad argument) in function 'putText'
> Overload resolution failed:
> - Can't convert object of type 'module' to 'str' for 'text'
> - Can't convert object of type 'module' to 'str' for 'text'
Even If I don’t put the temperature code into function It run the pyserial 1st before the opencv. Please Help I don’t know what to do. Please
Advertisement
Answer
presumably you want to sample your temperature 50 times and then return a single value?
def get_temp(ser, num_samples=50):
float_vals = [float(ser.readline()) for _ in range(num_samples)]
avg_val = sum(float_vals)/len(float_vals)
return str(avg_val) # convert to string for open cv
then in your opencv call use get_temp(ser)
instead of tempe
where ser
is a serial instance thats already open
if taking 50 samples is too slow then you can always take less samples with get_temp(ser,5)
to only take 5 samples for example … if you want the mode or median instead of the mean then i would recommend just using numpy.mode
or numpy.median
instead of calculating it (its probably faster to use numpy.mean
than calculating the average manually)