Untitled
unknown
python
a year ago
6.8 kB
7
Indexable
import cv2
import numpy as np
import os
import random
import time
from PIL import Image, ImageDraw, ImageFont
# Kullanıcı tarafından belirlenen parametreler
speed_level = 1
object_count = 6
display_duration = 60
# Türev parametreler
canvas_w, canvas_h = 1100, 600
object_size = 100
object_spacing = 10
emoji_folder = "emojis/"
background_color = (255, 255, 255)
frame_color = (211, 211, 211)
font_path = "dejavu-sans.ttf"
# Hız ayarları
speed_settings = {
1: 750,
2: 500,
3: 250
}
# Kullanıcı seçimine göre hız
speed = speed_settings[speed_level]
# Emojileri yükleyen fonksiyon
def load_emojis(emoji_folder):
emojis = []
for file_name in os.listdir(emoji_folder):
if file_name.endswith('.png'):
emoji_path = os.path.join(emoji_folder, file_name)
img = cv2.imread(emoji_path, cv2.IMREAD_UNCHANGED)
if img is not None and img.shape[2] == 4:
emojis.append(img)
else:
print(f"Failed to load: {file_name}")
return emojis
# Resmi tuval üzerine bindirme fonksiyonu
def overlay_image(background, img, position):
x, y = position
h, w = img.shape[:2]
if y + h > background.shape[0] or x + w > background.shape[1]:
return
alpha_img = img[:, :, 3] / 255.0
alpha_bg = 1.0 - alpha_img
for c in range(0, 3):
background[y:y+h, x:x+w, c] = (alpha_img * img[:, :, c] +
alpha_bg * background[y:y+h, x:x+w, c])
# Türkçe karakterleri destekleyen metin çizim fonksiyonu
def draw_text_with_pil(image, text, position, font_path, font_size, color):
pil_img = Image.fromarray(image)
draw = ImageDraw.Draw(pil_img)
font = ImageFont.truetype(font_path, font_size)
draw.text(position, text, font=font, fill=color)
return np.array(pil_img)
# Kullanıcı seçim kontrolü ve geri bildirim fonksiyonu
def check_selection(selected_index, correct_index, selected_emojis, question_frame, start_x, question_indices):
feedback_color = (0, 255, 0) if selected_index == correct_index else (0, 0, 255)
for i, idx in enumerate(question_indices):
emoji_x = start_x + i * (object_size + object_spacing)
emoji_y = canvas_h // 2
if idx == correct_index:
cv2.circle(question_frame, (emoji_x + object_size // 2, emoji_y + object_size + 30), 20, (0, 255, 0), 2)
cv2.line(question_frame, (emoji_x + object_size // 2 - 10, emoji_y + object_size + 20), (emoji_x + object_size // 2 + 10, emoji_y + object_size + 40), (0, 255, 0), 2)
cv2.line(question_frame, (emoji_x + object_size // 2 + 10, emoji_y + object_size + 20), (emoji_x + object_size // 2 - 10, emoji_y + object_size + 40), (0, 255, 0), 2)
elif idx == selected_index:
cv2.line(question_frame, (emoji_x + object_size // 2 - 10, emoji_y + object_size + 20), (emoji_x + object_size // 2 + 10, emoji_y + object_size + 40), (0, 0, 255), 2)
cv2.line(question_frame, (emoji_x + object_size // 2 + 10, emoji_y + object_size + 20), (emoji_x + object_size // 2 - 10, emoji_y + object_size + 40), (0, 0, 255), 2)
cv2.imshow("Feedback", question_frame)
cv2.waitKey(2000)
cv2.destroyWindow("Feedback")
# Rastgele soru ve seçenekleri gösteren fonksiyon
def ask_question(selected_emojis):
questions = [
"Hangi nesne yanındaydı?",
"İlk nesne neydi?",
"Son nesne neydi?",
"Ekrana en çok gelen nesne neydi?",
"İlk nesnenin sağında ne vardı?",
"Son nesnenin solunda ne vardı?"
]
question_indices = random.sample(range(len(selected_emojis)), 3)
correct_index = random.choice(question_indices)
question = random.choice(questions)
question_frame = np.full((canvas_h, canvas_w, 3), background_color, dtype=np.uint8)
question_frame = draw_text_with_pil(question_frame, question, (50, 50), font_path, 32, (0, 0, 0))
start_x = (canvas_w - (object_size + object_spacing) * len(question_indices) + object_spacing * (len(question_indices) - 1)) // 2
for i, idx in enumerate(question_indices):
emoji = selected_emojis[idx]
resized_emoji = cv2.resize(emoji, (object_size, object_size), interpolation=cv2.INTER_AREA)
x = start_x + i * (object_size + object_spacing)
y = canvas_h // 2
overlay_image(question_frame, resized_emoji, (x, y))
question_frame = draw_text_with_pil(question_frame, str(i + 1), (x + object_size // 2, y + object_size + 30), font_path, 32, (0, 0, 0))
cv2.imshow("Question", question_frame)
def mouse_callback(event, x, y, flags, param):
if event == cv2.EVENT_LBUTTONDOWN:
for i, idx in enumerate(question_indices):
emoji_x = start_x + i * (object_size + object_spacing)
emoji_y = canvas_h // 2
if emoji_x <= x <= emoji_x + object_size and emoji_y <= y <= emoji_y + object_size:
cv2.destroyWindow("Question")
check_selection(idx, correct_index, selected_emojis, question_frame, start_x, question_indices)
return
cv2.setMouseCallback("Question", mouse_callback)
cv2.waitKey(0)
cv2.destroyWindow("Question")
# Emojileri gösteren fonksiyon
def emoji_display():
emojis = load_emojis(emoji_folder)
if not emojis:
print("Failed to load emojis!")
return
start_time = time.time()
end_time = start_time + display_duration
shown_emojis = []
while time.time() < end_time:
frame = np.full((canvas_h, canvas_w, 3), background_color, dtype=np.uint8)
selected_emojis = random.sample(emojis, object_count)
shown_emojis.extend(selected_emojis)
frame_height = object_size + 2 * object_spacing
frame_width = (object_size + object_spacing) * object_count - object_spacing
start_x = (canvas_w - frame_width) // 2
start_y = (canvas_h - frame_height) // 2
cv2.rectangle(frame, (start_x, start_y), (start_x + frame_width, start_y + frame_height), frame_color, -1)
for i, emoji in enumerate(selected_emojis):
resized_emoji = cv2.resize(emoji, (object_size, object_size), interpolation=cv2.INTER_AREA)
x = start_x + i * (object_size + object_spacing)
y = start_y + object_spacing
overlay_image(frame, resized_emoji, (x, y))
cv2.imshow("Emoji Display", frame)
if cv2.waitKey(speed) & 0xFF == ord('q'):
cv2.destroyAllWindows()
return
cv2.destroyAllWindows()
ask_question(shown_emojis)
emoji_display()
Editor is loading...
Leave a Comment