Skip to content

Commit

Permalink
Update detect.py
Browse files Browse the repository at this point in the history
class  text ์ถ”์ถœ
text ์ถ”์ถœํ•œ๊ฑธ ์Œ์„ฑ์œผ๋กœ ์ถœ๋ ฅ

Signed-off-by: Leedong414 <165615367+Leedong414@users.noreply.github.com>
  • Loading branch information
Leedong414 committed May 28, 2024
1 parent 4e32dd2 commit ede96a6
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions detect.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,57 @@

import torch

from gtts import gTTS



def detect(opt):
source, weights, conf, save_txt_path = opt.source, opt.weights, opt.conf, opt.save_txt_path

# ๋ชจ๋ธ ๋กœ๋“œ
model = torch.hub.load('ultralytics/yolov5', 'custom', path=weights)

# ์ด๋ฏธ์ง€ ๋กœ๋“œ ๋ฐ ์ถ”๋ก  ์ˆ˜ํ–‰
results = model(source)

# ๊ฒฐ๊ณผ๋ฅผ pandas ๋ฐ์ดํ„ฐํ”„๋ ˆ์ž„์œผ๋กœ ๋ณ€ํ™˜
results_df = results.pandas().xyxy[0]

# ๋ฐ์ดํ„ฐํ”„๋ ˆ์ž„์˜ ํด๋ž˜์Šค ์ปฌ๋Ÿผ ์ถ”์ถœ
classes = results_df['name'].tolist()

# ํด๋ž˜์Šค ์ •๋ณด๋ฅผ ํ…์ŠคํŠธ ํŒŒ์ผ๋กœ ์ €์žฅ
with open(save_txt_path, 'w') as f:
for cls in classes:
f.write(f"{cls}\n")

# ์ฝ˜์†”์— ์ถœ๋ ฅ
print("Detected Classes:")
for cls in classes:
print(cls)

# TTS๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํด๋ž˜์Šค ์ด๋ฆ„๋“ค์„ ์Œ์„ฑ์œผ๋กœ ๋ณ€ํ™˜
if classes:
text_to_speak = 'Detected classes are: ' + ', '.join(classes)
tts = gTTS(text=text_to_speak, lang='en')
tts.save("detected_classes.mp3")
os.system("mpg321 detected_classes.mp3") # mpg321 ์„ค์น˜ ํ•„์š” (๋ฆฌ๋ˆ…์Šค์˜ ๊ฒฝ์šฐ)

if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('--weights', type=str, default='./runs/train/HongRyeon_yolov5s_results/weights/best.pt', help='model.pt path')
parser.add_argument('--source', type=str, default='/HongRyeon_test01.jpg', help='source') # file/folder, 0 for webcam
parser.add_argument('--conf', type=float, default=0.5, help='object confidence threshold')
parser.add_argument('--save_txt_path', type=str, default='detected_classes.txt', help='path to save detected classes txt file')
opt = parser.parse_args()

with torch.no_grad():
detect(opt)





FILE = Path(__file__).resolve()
ROOT = FILE.parents[0] # YOLOv5 root directory
if str(ROOT) not in sys.path:
Expand Down

0 comments on commit ede96a6

Please sign in to comment.