Skip to content

Commit

Permalink
Add AI examples in NeoSoco book
Browse files Browse the repository at this point in the history
  • Loading branch information
JeongJun-Lee committed Oct 8, 2024
1 parent 09cbe64 commit a6fc099
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 13 deletions.
19 changes: 19 additions & 0 deletions mu/resources/neopia/03-01_AI_Radiokarnay.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from neopia import *

n = Neosoco()
result = False

while not result:
try:
Voice.tts("I'm hearing. Please speak in 3 secconds.")
print("Eshityapman. 3 soniya ichida gapirib bering.")
result = Voice.stt()
print(result)
if result == "Chiroqni yoq":
n.led_on()
wait(3000)
elif result == "Chiroqni o'chir":
n.led_off()
wait(3000)
except:
result = False
48 changes: 48 additions & 0 deletions mu/resources/neopia/03-02_AI_Kamera.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
from pgzhelper import *
from pgzhelper import Util
from neopia import *

n = Neosoco()
fd = FaceDetection()

WIDTH = 640
HEIGHT = 480
pygame_img = None
detected = False
red_led = Actor('red_led_off', (600, 60))
red_led.images = ['red_led_on', 'red_led_off']
red_led.scale = 0.5
red_led.fps = 15

fd = FaceDetection()
if fd.camera_open(1) is False:
game.exit()

def draw():
if detected:
screen.blit(pygame_img, (0, 0))
red_led.draw()
n.led_on()
n.buzzer()
n.led_off()
else:
screen.blit('living_room', (0, 0))

def update():
global pygame_img
global detected

frame, faces = fd.start_detection(just_rtn_frame=True)
pygame_img = Util.opencv_to_pygame_img(frame)

if faces > 0:
detected = True
red_led.next_image()
print('Detected!!!')
else:
detected = False


def on_key_down(key):
if key == keys.SPACE:
game.exit()
17 changes: 17 additions & 0 deletions mu/resources/neopia/03-03_AI_Obyekt_topish.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from neopia import *

n = Neosoco()
od = ObjectDetection()

if od.camera_open(0):
while True:
result = od.start_detection()
if result:
obj_names, obj_coords = result
if 'sports ball' in obj_names:
n.led_on('out1', '100')
n.motor_rotate('both', 'forward', '10')
wait(1000)
n.led_off('out1')
else:
n.motor_rotate('both', 'left', '10')
29 changes: 16 additions & 13 deletions mu/resources/neopia/04-03-02_Aqlli_avtomobil_2.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
from neopia import *

n = Neosoco()
chap_m = 0
ong_m = 0

# Masofa sensor orqali to‘siqni tekshirish uchun funksiya yaratish
def servo_motor():
n.motor_stop('both')
n.servo_reset_degree('out3')
wait(100)
n.servo_rotate_by_degree('out3', 'forward', '100', '90')
wait(1000)
global chap_m
chap_m = n.get_value('in2')
n.servo_rotate_by_degree('out3', 'backward', '100', '90')
wait(1000)
global ong_m
ong_m = n.get_value('in2')
n.servo_rotate_by_degree('out3', 'forward', '100', '0')
wait(1000)
global chap_m
global ong_m

n.motor_stop('both')
n.servo_reset_degree('out3')
wait(100)
n.servo_rotate_by_degree('out3', 'forward', '100', '90')
wait(1000)
chap_m = n.get_value('in2')
n.servo_rotate_by_degree('out3', 'backward', '100', '90')
wait(1000)
ong_m = n.get_value('in2')
n.servo_rotate_by_degree('out3', 'forward', '100', '0')
wait(1000)

# Funksiyadan foydalanib dastur tuzish
while True:
Expand Down

0 comments on commit a6fc099

Please sign in to comment.