Code:
import cv2
import mediapipe as mp
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.textinput import TextInput
from kivy.uix.boxlayout import BoxLayout
import pyttsx3
class handTracker():
def __init__(self, mode=False, maxHands=2, detectionCon=0.5,modelComplexity=1,trackCon=0.5):
self.mode = mode
self.maxHands = maxHands
self.detectionCon = detectionCon
self.modelComplex = modelComplexity
self.trackCon = trackCon
self.mpHands = mp.solutions.hands
self.hands = self.mpHands.Hands(self.mode, self.maxHands,self.modelComplex,
self.detectionCon, self.trackCon)
self.mpDraw = mp.solutions.drawing_utils
def handsFinder(self,image,draw=True):
imageRGB = cv2.cvtColor(image,cv2.COLOR_BGR2RGB)
self.results = self.hands.process(imageRGB)
if self.results.multi_hand_landmarks:
for handLms in self.results.multi_hand_landmarks:
if draw:
self.mpDraw.draw_landmarks(image, handLms, self.mpHands.HAND_CONNECTIONS)
return image
def positionFinder(self,image, handNo=0, draw=True):
lmlist = []
if self.results.multi_hand_landmarks:
Hand = self.results.multi_hand_landmarks[handNo]
for id, lm in enumerate(Hand.landmark):
h,w,c = image.shape
cx,cy = int(lm.x*w), int(lm.y*h)
lmlist.append([id,cx,cy])
if draw:
cv2.circle(image,(cx,cy), 15 , (255,0,255), cv2.FILLED)
return lmlist
def main():
cap = cv2.VideoCapture(0)
tracker = handTracker()
while True:
success,image = cap.read()
image = tracker.handsFinder(image)
lmList = tracker.positionFinder(image)
if len(lmList) != 0:
print(lmList[4])
import speech_recognition as sr
recognizer = sr.Recognizer()
with sr.Microphone() as source:
print("Adjusting noise ")
recognizer.adjust_for_ambient_noise(source, duration=1)
print("Recording for 4 seconds")
recorded_audio = recognizer.listen(source, timeout=4)
print("Done recording")
try:
print("Recognizing the text")
text = recognizer.recognize_google(
recorded_audio,
language="en-US"
)
with open("history.csv","a") as f:
f.write("{}".format(text))
print("Successfully")
except Exception as ex:
print(ex)
exit()
cv2.imshow("Video",image)
cv2.waitKey(1)
class Main(App):
def build(self):
self.al_speak = pyttsx3.init()
self.getname = False
main_layout = BoxLayout(orientation="vertical")
button = Button(text='Click',size_hint=(.1, .1),pos_hint={'center_x': .5, 'center_y': .5})
button.bind(on_press=self.get_name)
self.textinput = TextInput(text='Hello world',size_hint=(.5, .5),pos_hint={'center_x': .5, 'center_y': .5})
main_layout.add_widget(self.textinput)
main_layout.add_widget(button)
return main_layout
def get_name(self, instance):
if self.getname is False:
self.getname = True
self.al_speak.say("Hello {} raise one hand above your head to speak.".format(self.textinput.text))
self.al_speak.say("Then, raise your other hand above your head to start searching.")
self.al_speak.runAndWait()
with open("history.csv","a+") as f:
f.write("\n"+self.textinput.text+",")
else:
print("You have already enter the name!")
main()
if __name__ == '__main__':
app = Main()
app.run()
Note:
+ Ngồi làm cả chiều muốn đập máy lắm đấy :)))
+ Không làm giống hoàn toàn đề bài được, mình chỉ làm được khi người dùng giơ tay vào khung hình thì sẽ bắt đầu ghi âm, sau 4 giây sẽ lưu dữ liệu (Định làm khi người dùng giơ tay lần nữa mới tắt ghi âm nhưng cái thư viện speech_recognition hôm nay cứ bị sao ý, không đặt giới hạn là không ghi âm được)
+ Định dùng pandas để xử lý file CSV nhưng lừa quá làm như file text luôn :)))
+ Các thư viện dùng: OpenCV, Mediapipe, Kivy, Pyttsx3, Speech_recognition (Cái này phải cày thêm cả Pyaudio nữa)
Tin học, tiếng Anh: informatics, tiếng Pháp: informatique, là một ngành khoa học chuyên nghiên cứu quá trình tự động hóa việc tổ chức, lưu trữ, xử lý và truyền dẫn thông tin của một hệ thống máy tính cụ thể hoặc trừu tượng (ảo). Với cách hiểu hiện nay, tin học bao hàm tất cả các nghiên cứu và kỹ thuật có liên quan đến việc mô phỏng, biến đổi và tái tạo thông tin.
Nguồn : Wikipedia - Bách khoa toàn thưLớp 7 - Năm thứ hai ở cấp trung học cơ sở, một cuồng quay mới lại đến vẫn bước tiếp trên đường đời học sinh. Học tập vẫn là nhiệm vụ chính!
Nguồn : ADMIN :))Copyright © 2021 HOCTAP247