(Đồ án tốt nghiệp) Ứng dụng xử lý ảnh vào mô hình điều khiển đèn giao thông ngã tư

109 166 2
(Đồ án tốt nghiệp) Ứng dụng xử lý ảnh vào mô hình điều khiển đèn giao thông ngã tư

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

(Đồ án tốt nghiệp) Ứng dụng xử lý ảnh vào mô hình điều khiển đèn giao thông ngã tư(Đồ án tốt nghiệp) Ứng dụng xử lý ảnh vào mô hình điều khiển đèn giao thông ngã tư(Đồ án tốt nghiệp) Ứng dụng xử lý ảnh vào mô hình điều khiển đèn giao thông ngã tư(Đồ án tốt nghiệp) Ứng dụng xử lý ảnh vào mô hình điều khiển đèn giao thông ngã tư(Đồ án tốt nghiệp) Ứng dụng xử lý ảnh vào mô hình điều khiển đèn giao thông ngã tư(Đồ án tốt nghiệp) Ứng dụng xử lý ảnh vào mô hình điều khiển đèn giao thông ngã tư(Đồ án tốt nghiệp) Ứng dụng xử lý ảnh vào mô hình điều khiển đèn giao thông ngã tư(Đồ án tốt nghiệp) Ứng dụng xử lý ảnh vào mô hình điều khiển đèn giao thông ngã tư(Đồ án tốt nghiệp) Ứng dụng xử lý ảnh vào mô hình điều khiển đèn giao thông ngã tư(Đồ án tốt nghiệp) Ứng dụng xử lý ảnh vào mô hình điều khiển đèn giao thông ngã tư(Đồ án tốt nghiệp) Ứng dụng xử lý ảnh vào mô hình điều khiển đèn giao thông ngã tư(Đồ án tốt nghiệp) Ứng dụng xử lý ảnh vào mô hình điều khiển đèn giao thông ngã tư(Đồ án tốt nghiệp) Ứng dụng xử lý ảnh vào mô hình điều khiển đèn giao thông ngã tư(Đồ án tốt nghiệp) Ứng dụng xử lý ảnh vào mô hình điều khiển đèn giao thông ngã tư(Đồ án tốt nghiệp) Ứng dụng xử lý ảnh vào mô hình điều khiển đèn giao thông ngã tư(Đồ án tốt nghiệp) Ứng dụng xử lý ảnh vào mô hình điều khiển đèn giao thông ngã tư(Đồ án tốt nghiệp) Ứng dụng xử lý ảnh vào mô hình điều khiển đèn giao thông ngã tư

BỘ GIÁO DỤC & ĐÀO TẠO TRƯỜNG ĐẠI HỌC SƯ PHẠM KỸ THUẬT TP HỒ CHÍ MINH KHOA ĐIỆN – ĐIỆN TỬ BỘ MÔN ĐIỆN TỬ CÔNG NGHIỆP – Y SINH - ĐỒ ÁN TỐT NGHIỆP NGÀNH CÔNG NGHỆ KỸ THUẬT ĐIỆN TỬ TRUYỀN THÔNG ĐỀ TÀI: ỨNG DỤNG XỬ LÝ ẢNH VÀO MÔ HÌNH ĐIỀU KHIỂN ĐÈN GIAO THƠNG NGÃ TƯ GVHD: ThS Nguyễn Duy Thảo SVTH 1: Nguyễn Tấn Hưng MSSV: 16141175 SVTH 2: Nguyễn Minh Tuấn MSSV: 16141330 Tp Hồ Chí Minh – 01/2021 BỘ GIÁO DỤC & ĐÀO TẠO TRƯỜNG ĐẠI HỌC SƯ PHẠM KỸ THUẬT TP HỒ CHÍ MINH KHOA ĐIỆN – ĐIỆN TỬ BỘ MÔN ĐIỆN TỬ CÔNG NGHIỆP – Y SINH - ĐỒ ÁN TỐT NGHIỆP NGÀNH CÔNG NGHỆ KỸ THUẬT ĐIỆN TỬ TRUYỀN THÔNG ĐỀ TÀI: ỨNG DỤNG XỬ LÝ ẢNH VÀO MƠ HÌNH ĐIỀU KHIỂN ĐÈN GIAO THƠNG NGÃ TƯ GVHD: ThS Nguyễn Duy Thảo SVTH 1: Nguyễn Tấn Hưng MSSV: 16141175 SVTH 2: Nguyễn Minh Tuấn MSSV: 16141330 Tp Hồ Chí Minh – 01/2021 LỜI CAM ĐOAN Đề tài nhóm đồ án tự nghiên cứu thực dựa vào số tài liệu trước khơng chép từ tài liệu hay cơng trình có trước Người thực đề tài Nguyễn Tấn Hưng – Nguyễn Minh Tuấn iv LỜI CẢM ƠN Trong thời gian thực đồ án tốt nghiệp, nhóm sinh viên thực đề tài học hỏi nhiều kiến thức bổ ích giáo trình thực tế từ thầy cô Qua em xin gửi lời cảm ơn chân thành đến: Quý thầy cô khoa điện điện tử trường Đại Học Sư Phạm Kỹ Thuật Thành phố Hồ Chí Minh truyền đạt kiến thức, kinh nghiệm quý báu chuyên môn lĩnh vực khác Sự tận tụy say mê, lịng nhân ái, nhiệt tình thầy động lực lớn giúp chúng em trau dồi thêm kiến thức giúp chúng em vượt qua khó khăn học tập sống Chúng em xin gửi lời cảm ơn chân thành đến thầy Nguyễn Duy Thảo giúp đỡ hướng dẫn chúng em hoàn thành đồ án tốt nghiệp Đồng thời cảm ơn đến tất bạn bè gắn bó giúp đỡ học tập suốt trình thực đồ án tốt nghiệp Xin chân thành cảm ơn! Người thực đề tài Nguyễn Tấn Hưng – Nguyễn Minh Tuấn v MỤC LỤC NHIỆM VỤ ĐỒ ÁN TỐT NGHIỆP I LỊCH TRÌNH THỰC HIỆN ĐỒ ÁN TỐT NGHIỆP II LỜI CAM ĐOAN IV LỜI CẢM ƠN V MỤC LỤC VI LIỆT KÊ HÌNH VẼ VIII LIỆT KÊ BẢNG XI TÓM TẮT XII CHƯƠNG TỔNG QUAN 1.1 ĐẶT VẤN ĐỀ 1.2 MỤC TIÊU .2 1.3 NỘI DUNG NGHIÊN CỨU 1.4 GIỚI HẠN 1.5 BỐ CỤC CHƯƠNG CƠ SỞ LÝ THUYẾT 2.1 GIỚI THIỆU VỀ XỬ LÝ ẢNH 2.2 NGƠN NGỮ LẬP TRÌNH PYTHON 2.3 MỘT SỐ THƯ VIỆN TRONG PYTHON 2.3.1 Thư viện lập trình giao diện PYQT5 .8 2.3.2 Thư viện xử lý ảnh OPENCV .9 2.4 TỔNG QUAN VỀ YOLO 12 2.4.1 Lịch sử phát triển từ YOLOv1 đến YOLOv5 .12 2.4.2 Lý thuyết YOLO 20 2.5 GIỚI THIỆU GOOGLE COLAB 26 2.6 KHÁI QUÁT VỀ ARDUINO 28 2.7 GIỚI THIỆU PHẦN CỨNG 33 2.7.1 Kit Arduino Uno R3 33 2.7.2 Led đơn 37 2.7.3 Camera 38 CHƯƠNG TÍNH TỐN VÀ THIẾT KẾ 39 3.1 GIỚI THIỆU 39 3.2 TÍNH TỐN VÀ THIẾT KẾ HỆ THỐNG 39 3.2.1 Thiết kế sơ đồ khối hệ thống 39 3.2.2 Thiết kế khối hệ thống 40 3.2.3 Thiết kế mơ hình đèn giao thơng 46 3.2.4 Sơ đồ hệ thống .47 vi CHƯƠNG THI CÔNG HỆ THỐNG 48 4.1 GIỚI THIỆU 48 4.2 THI CÔNG HỆ THỐNG 48 4.2.1 Thi công bo mạch 48 4.2.2 Lắp ráp kiểm tra 49 4.2.3 Huấn luyện Yolo Google Colab 49 4.2.4 Thiết kế giao diện điều khiển 55 4.3 ĐĨNG GĨI VÀ THI CƠNG MƠ HÌNH 57 4.4 LẬP TRÌNH HỆ THỐNG 60 4.4.1 Lập trình điều khiển máy tính 60 4.4.2 Lập trình Kit Arduino Uno .63 4.5 VIẾT TÀI LIỆU HƯỚNG DẪN SỬ DỤNG, THAO TÁC 73 4.5.1 Viết tài liệu hướng dẫn sử dụng 73 4.5.2 Quy trình thao tác giao diện 74 CHƯƠNG KẾT QUẢ_NHẬN XÉT_ĐÁNH GIÁ 77 5.1 KẾT QUẢ 77 5.2 NHẬN XÉT 83 5.3 ĐÁNH GIÁ 83 CHƯƠNG KẾT LUẬN VÀ HƯỚNG PHÁT TRIỂN .84 6.1 KẾT LUẬN 84 6.2 HƯỚNG PHÁT TRIỂN .84 TÀI LIỆU THAM KHẢO 86 PHỤ LỤC .89 vii LIỆT KÊ HÌNH VẼ Hình Trang Hình 2.1: Biểu tượng ngơn ngữ lập trình Python Hình 2.2: Biểu tượng Qt Hình 2.3: Biểu tượng thư viện OpenCV Hình 2.4: Cách YOLOv1 dự đốn .12 Hình 2.5: Kiến trúc YOLOv1 13 Hình 2.6: Dự đoán bounding box YOLOv2 .15 Hình 2.7: Kiến trúc YOLOv2 16 Hình 2.8: Kỹ thuật Reorg YOLOv2 17 Hình 2.9: Kiến trúc YOLOv3 [21] 18 Hình 2.10: So sánh YOLOv4 với số mơ hình khác 19 Hình 2.11: Ảnh phân chia grid size 7x7 .22 Hình 2.12: YOLOv1 dự đốn bouding box xung quanh object 22 Hình 2.13: Vector ngõ thuật tốn YOLO 23 Hình 2.14: Kiến trúc mạng CNN dùng YOLOv1 .23 Hình 2.15: Hai box giao 26 Hình 2.16: Google colab 26 Hình 2.17: Board Arduino UNO R3 29 Hình 2.18: Board Arduino Nano 29 Hình 2.19: Board Arduino Lilypad 30 Hình 2.20: Board Arduino Redboard 30 Hình 2.21: Board Arduino Mega R3 31 Hình 2.22: Board Arduino Leonardo 31 Hình 2.23: Máy in 3D Materia 101 32 Hình 2.24: Xe điều khiển từ xa 32 Hình 2.25: Arduino Uno R3 33 Hình 2.26: Giao tiếp hệ thống dùng chuẩn UART 35 Hình 2.27: Nguyên lý hoạt động chuẩn UART 36 Hình 2.28: Led đơn sắc 37 Hình 2.29: Sơ đồ chân led đơn sắc 37 Hình 2.30: Camera .38 Hình 3.1: Sơ đồ khối hệ thống .39 Hình 3.2: Camera kết nối với máy tính 40 Hình 3.3: Sơ đồ nguyên lý khối nguồn board Arduino Uno 41 Hình 3.4: Sơ đồ nguyên lý mạch dao động board Arduino Uno 42 Hình 3.5: Sơ đồ nguyên lý mạch reset board Arduino Uno 43 Hình 3.6: Sơ đồ nguyên lý mạch nạp board Arduino Uno 44 Hình 3.7: Led 5mm màu đỏ, vàng, xanh thực tế 44 Hình 3.8: Sơ đồ kết nối led đơn với điện trở 45 viii Hình 3.9: Cầu chì MF-MSMF050-2 46 Hình 3.10: Mặt mơ hình sau thiết kế 46 Hình 3.11: Sơ đồ hệ thống 47 Hình 4.1: Sơ đồ nguyên lý mạch hiển thị 48 Hình 4.2: Sơ đồ mạch in 48 Hình 4.3: Sơ đồ mạch thi công .49 Hình 4.4: Hình ảnh thực tế sau thi công 49 Hình 4.5: Bộ dataset dùng để huấn luyện YOLO 50 Hình 4.6: Giao diện labelImg sau khởi động 50 Hình 4.7: Tiến hành gán nhãn cho ảnh 51 Hình 4.8: File train.txt chứa đường dẫn ảnh dùng để huấn luyện 52 Hình 4.9: Thư mục ML tạo Drive 53 Hình 4.10: Giao diện thiết kế Qt Designer 55 Hình 4.11: Xuất file python từ giao diện thiết kế Qt Designer 55 Hình 4.12: Giao diện điều khiển 56 Hình 4.13: Hình 3D mặt mơ hình 57 Hình 4.14: Hình 3D mặt bên mơ hình 57 Hình 4.15: Hình 3D mặt bên có rãnh đưa USB ngồi .58 Hình 4.16: Hình 3D trụ giá đỡ gắn camera .58 Hình 4.17: Hình 3D mơ hình hồn chỉnh .59 Hình 4.18: Mặt mơ hình sau thi cơng .59 Hình 4.19: Mặt mơ hình sau thi cơng 60 Hình 4.20: Sơ đồ khối hệ thống điều khiển máy tính 60 Hình 4.21: Lưu đồ giải thuật giao diện điều khiển máy tính 61 Hình 4.22: Biểu tượng PyCharm .62 Hình 4.23: Sơ đồ khối hệ thống điều khiển arduino .63 Hình 4.24: Lưu đồ giải thuật arduino .64 Hình 4.25: Khảo sát môi trường phát triển phổ biến năm 2019 65 Hình 4.26: Giao diện tính Git Visual Studio Code 66 Hình 4.27: Giao diện terminal Visual Studio Code 67 Hình 4.28: Giao diện Visual Studio Code lúc cài đặt 68 Hình 4.29: Một số extension ngơn ngữ lập trình phổ biến 68 Hình 4.30: Kho extension Visual Studio Code .69 Hình 4.31: Các extension công ty công nghệ người dùng đóng góp 69 Hình 4.32: Các cập nhật Visual Studio Code 70 Hình 4.33: Giao điện visual studio code khởi động .71 Hình 4.34: Cài đặt FlatformIO .71 Hình 4.35: Giao điện FlatformIO 72 Hình 4.36: Tạo dự án FlatformIO 72 Hình 4.37: Giao diện tạo project thành công 73 Hình 4.38: Hướng dẫn sử dụng giao diện điều khiển 74 Hình 4.39: Giao diện điều khiển vừa khởi động 74 ix Hình 4.40: Giao diện điều khiển chế độ MANUAL 75 Hình 4.41: Giao diện điều khiển chế độ tự động mặc định 75 Hình 4.42: Giao diện điều khiển chế độ tự động có dùng YOLO 76 Hình 5.1: Hình 3D mơ hình hồn chỉnh .77 Hình 5.2: Sơ đồ nguyên lý mạch hiển thị 77 Hình 5.3: Sơ đồ mạch in 77 Hình 5.4: Mơ hình hồn chỉnh cho chạy demo .78 Hình 5.5: Giao diện điều khiển 78 Hình 5.6: Mơ hình vừa khởi động 79 Hình 5.7: Giao diện điều khiển vừa khởi động 79 Hình 5.8: Mơ hình giao lộ xe 80 Hình 5.9: Giao diện điều khiển giao lộ xe .80 Hình 5.10: Mơ hình giao lộ đông xe 81 Hình 5.11: Giao diện điều khiển giao lộ đông xe .81 x LIỆT KÊ BẢNG Bảng Trang Bảng 2.1: Thông số kỹ thuật Arduino Uno R3 35 Bảng 3.1: Liệt kê điện áp dòng điện linh kiện sử dụng mạch 45 Bảng 4.1: Danh sách linh kiện .48 Bảng 5.1: Kết nhận dạng .82 xi CHƯƠNG KẾT LUẬN VÀ HƯỚNG PHÁT TRIỂN Chương KẾT LUẬN VÀ HƯỚNG PHÁT TRIỂN 6.1 KẾT LUẬN Sau khoảng thời gian 15 tuần kể từ chúng em nhận tìm hiểu sau thực đề tài: “Ứng dụng xử lý ảnh vào mơ hình điều khiển đèn giao thơng ngã tư”, nhóm đạt khoảng 99% mục tiêu đặt ban đầu Cụ thể nhóm chúng em đạt kết sau: - Thiết kế giao diện sử dụng phần mềm Qt Designer cho phép đếm số lượng phương tiện, hiển thị đường thời gian đèn tín hiệu sáng đường Tích hợp nút nhấn điều khiển đèn tín hiệu - Giao tiếp máy tính Arduino từ điều khiển thiết bị ngoại vi - Nhận dạng đếm số lượng phương tiện tham gia giao thông đường - Thi công hệ thống ứng dụng xử lý ảnh để điều tiết giao thơng ngã tư với ngõ vào hình ảnh chụp từ camera cao - Mơ hình điều khiển tín hiệu đèn giao thơng đạt kết đáng tin cậy - Thời gian sáng đèn điều khiển hợp lý theo yêu cầu đặt đề tài tuyến đường có lưu lượng xe lưu thơng lớn tuyến đường ưu tiên sáng đèn xanh lâu Đây ưu điểm cải tiến so với hệ thống đèn giao thông Tuy nhiên điều thực tế khó thực bên cạnh với kết đạt hệ thống có hạn chế xe sát nhau, hệ thống đếm đối tượng thành chung với Để khắc phục điều cần đặt camera vng góc với mặt đường độ cao vừa phải, đồng thời sử dụng loại camera có độ phân giải cao, hạn chế rung lắc kết ảnh phù hợp cho việc xử lý 6.2 HƯỚNG PHÁT TRIỂN Với hệ thống mang tính chất ứng dụng vào thực tiễn cách cao hơn, thân chúng em người thực nên cải tiến thêm hướng: - Nâng cấp camera phần mềm để nhận diện xe ưu tiên - Nâng cấp hệ thống hiển thị tốc độ lưu thơng phương tiện hình giám sát BỘ MÔN ĐIỆN TỬ CÔNG NGHIỆP – Y SINH 84 CHƯƠNG KẾT LUẬN VÀ HƯỚNG PHÁT TRIỂN - Xây dựng trạm xử lý trung tâm để điều khiển đồng đèn giao thông để giải tốt vấn đề ùn tắc giao thông - Ngồi việc chụp hình phương tiện dừng đèn đỏ để tính số giây đèn xanh, cịn cải tiến camera phát chụp biển số phương tiện vượt đèn đỏ để tiến hành xử lý vi phạm BỘ MÔN ĐIỆN TỬ CÔNG NGHIỆP – Y SINH 85 TÀI LIỆU THAM KHẢO TÀI LIỆU THAM KHẢO [1] Nguyễn Thanh Hải, “Giáo Trình Xử Lý Ảnh”, Trường ĐHSPKT, Tp.HCM, Nhà xuất ĐH Quốc Gia, 2015 [2] Zhong-Qiu Zhao, Peng Zheng, Shou-tao Xu, and Xindong Wu, “Object Detection with Deep Learning: A Review”, Institute of Electrical and Electronics Engineers, 16 Apr 2019 [3] Hans-Petter Halvorsen, “Introduction to ARDUINO”, 2008 [4] Phil Spector, “Introduction to Python Programming”, Department of Statistics, University of California Berkeley, March 16, 2005 [5] Dave Kuhlman, “A Python Book: Beginning Python, Advanced Python, and Python Exercises”, Dec 2013 [6] J R Parker, “Algorithms for Image Processing and Computer Vision”, 2st Edition, 2010 [7] Lin, H., K M Aye, H.M Tun, Theingi and Z M Naing, “Design and Construction of IntelligentTraffic Light Control System Using FuzzyLogic”, Proc AIP Conf., Vol 1052, pp 237-239, 2008 [8] Wiering M., J.v Veenen, J Vreeken and A Koopman, “Intelligent Traffic Light Control”, Technical report UU – CS – 2004 – 029, UtrechtUniversity, The Netherlands, 2004 [9] Tan K K., M Khalid and R Yusof, “IntelligentTraffic Lights Control by Fuzzy Logic”, Malaysian Journal of Computer Science, Vol 9No 2, pp 29-35, 1996 [10] Kulkarni, G.H.; Waingankar, P.G, “Fuzzy logicbased traffic light controller”, Proc IEEE Conf ICIIS 2007, Issue 9-11 Aug 2007 pp.107 – 110 [11] AdvanTech, “Digital Traffic Light Detection System”, Advantech, 2007 http://www.advantech.com/Network/CMScontent-apps-8.asp.html/ [12] Ngọc Ẩn, “Đèn tín hiệu giao thơng mới: “Phơi nắng” 3,5 triệu đơla!”, Báo Tuổi Trẻ, số 21/12/2004 BỘ MÔN ĐIỆN TỬ CÔNG NGHIỆP – Y SINH 86 TÀI LIỆU THAM KHẢO [13] Lê Mỹ, “Gần 50 chốt đèn giao thông lắp hệ thống cảm biến”, Báo Dân Trí, số ngày 17/12/2007 [14] Ánh Nguyệt, Thu Hồng, “48 đèn tín hiệu giao thông mắc chứng “nan y””, Báo Người Lao Động, số ngày 20/10/2008 [15] “Google Colab”, http://tutorialspoint.com/ 2019 [16] Quoc Pham, “Tìm Hiểu Mơ Hình YOLO Cho Bài Toán Object Detection – Understanding YOLO”, Data Scientist at Overspace, 2018 https://pbcquoc.github.io/yolo/ [17] Joseph Redmon, Santosh Divvala, Ross Girshick, Ali Farhadi, “You Only Look Once: Unified, Real-Time Object Detection”, University of Washington, Allen Institute for AI, 2016 [18] Joseph Redmon, Ali Farhadi, “YOLO9000: Better, Faster, Stronger”, University of Washington, Allen Institute for AI, 2017 [19] Joseph Redmon, Ali Farhadi, “YOLOv3: An Incremental Improvement”, University of Washington, 2018 [20] Alexey Bochkovskiy, Chien-Yao Wang, Hong-Yuan Mark Liao, “YOLOv4: Optimal Speed and Accuracy of Object Detection”, Institute of Information Science Academia Sinica, Taiwan, April 2020 [21] Yi-Qi Huang, Jia-Chun Zheng, Shi-Dan Sun, Cheng-Fu Yang and Jing Liu, “Optimized YOLOv3 Algorithm and Its Application in Traffic Flow Detections”, 28 April 2020 [22] Lei Mao, “YOLO v2 Reorg Layer Explained”, 24 April 2019 [23] Seonkyeong Seong, Jeongheon Song, Donghyeon Yoon, Jiyoung Kim and Jaewan Choi, “Determination of Vehicle Trajectory through Optimization of Vehicle Bounding Boxes Using a Convolutional Neural Network”, 30 September 2019 [24] TAPIT, “Arduino Board: Phần cứng board mạch Arduino Uno R3”, Tapit, 04 – 2019 https://tapit.vn/tim-hieu-phan-cung-board-mach-arduino-uno-r3/ BỘ MÔN ĐIỆN TỬ CÔNG NGHIỆP – Y SINH 87 TÀI LIỆU THAM KHẢO [25] Joseph Howse, “OpenCV Computer Vision with Python”, 2013 [26] Nguyễn Thái Dương Duy, “Thiết kế thi công hệ thống điều khiển tín hiệu đèn giao thơng thơng qua xử lý ảnh”, Đồ án tốt nghiệp, trường ĐHSPKT, Tp.HCM, 2018 [27] Martin Fitzpatrick, “Create GUI Applications with Python & Qt5”, Version 4.0, 2020-07-01 [28] ViHoth Corporation, “Phần mềm PyCharm, IDE hàng đầu cho phát triển Python Django chuyên nghiệp”, https://vihoth.com/, 2019 https://vihoth.com/phan-mem-van-phong/jetbrains/phan-mem-pycharm-idehang-dau-cho-su-phat-trien-python-va-django-chuyen-nghiep/ BỘ MÔN ĐIỆN TỬ CÔNG NGHIỆP – Y SINH 88 PHỤ LỤC PHỤ LỤC ❖ Chương trình điều khiển Arduino: #include #include #define green1 #define yellow1 #define red1 #define green2 #define yellow2 #define red2 const uint8_t green_time = 15; const uint8_t yellow_time = 3; const uint8_t plus_time = 5; uint8_t count_add = 0; boolean priority = 0, start = false, run1 = true, set = true; String buff; volatile uint8_t count = 2*(green_time + yellow_time), plus = 0; void light_control(uint8_t x) { digitalWrite(green1, x & 0x01); digitalWrite(yellow1, (x>>1) & 0x01); digitalWrite(red1, (x>>2) & 0x01); digitalWrite(green2, (x>>4) & 0x01); digitalWrite(yellow2, (x>>5) & 0x01); digitalWrite(red2, (x>>6) & 0x01); } void ISR_timer (void) { if(start) { if(count > 1) { if (start) count ; if( count == green_time) count_add = 0; } else { count = 2*(green_time + yellow_time); count_add = 0; } }else { if((count 0)) count ; } } void reset_counter(void) { count_add = 0; count = 2*(green_time + yellow_time); } void setup() { // put your setup code here, to run once: pinMode(green1, OUTPUT); pinMode(yellow1, OUTPUT); pinMode(red1, OUTPUT); pinMode(green2, OUTPUT); BỘ MÔN ĐIỆN TỬ CÔNG NGHIỆP – Y SINH 89 PHỤ LỤC pinMode(yellow2, OUTPUT); pinMode(red2, OUTPUT); if(!start) light_control(0x00); Serial.begin(9600); Timer1.initialize(1000000); Timer1.attachInterrupt(ISR_timer); } void loop() { if (Serial.available()) //Nếu có tín hiệu từ Pi { buff = Serial.readStringUntil('\r'); //Đọc vào đến gặp \r (xuống dòng) if (buff=="auto") { start = true; reset_counter(); Serial.println(static_cast (count)); }else if (buff=="manual") { start = false; count = yellow_time + 2; }else if(buff == "run1") { if(!start) { if (set || (!run1)) { run1 = true; count = yellow_time; set = false; } } }else if(buff == "run2") { if(!start) { if(run1) { run1 = false; count = yellow_time; } } }else if(buff == "add") { if(count_add < 3) { count_add++; count += plus_time; }else { count_add = 0; } } } if(start) { Serial.println(static_cast (count)); if(count > (green_time + 2*yellow_time)) light_control(0x41); else if (count > (yellow_time + green_time)) light_control(0x42); else if (count > yellow_time) light_control(0x14); BỘ MÔN ĐIỆN TỬ CÔNG NGHIỆP – Y SINH 90 PHỤ LỤC else light_control(0x24); }else { if(run1) { if(count > yellow_time) light_control(0x22); else if(count > 0) light_control(0x24); else light_control(0x41); }else { if(count > yellow_time) light_control(0x22); else if(count > 0) light_control(0x42); else light_control(0x14); } } } ❖ Chương trình giao diện máy tính: from PyQt5 import QtCore, QtGui, QtWidgets import cv2 import numpy as np from MySerial import MySerialClass class Ui_MainWindow(object): def setupUi(self, MainWindow): MainWindow.setObjectName("MainWindow") MainWindow.resize(1500, 710) MainWindow.setDocumentMode(False) self.default = False self.auto = True self.gtime = 15 self.ytime = self.ptime = self.rtime = self.gtime + self.ytime self.car = self.bus = self.truck = self.threshold = # Load Yolo self.net = cv2.dnn.readNet("yolov3_5000.weights", "yolov3.cfg") self.classes = [] with open("yolo.names", "r") as f: self.classes = [line.strip() for line in f.readlines()] self.layer_names = self.net.getLayerNames() self.output_layers = [self.layer_names[i[0] - 1] for i in self.net.getUnconnectedOutLayers()] self.colors = [[255,0,0],[0,255,0],[0,0,255]]#np.random.uniform(0, 255, size=(len(self.classes), 3)) self.centralwidget = QtWidgets.QWidget(MainWindow) self.centralwidget.setObjectName("centralwidget") self.lcdNumber = QtWidgets.QLCDNumber(self.centralwidget) self.lcdNumber.setGeometry(QtCore.QRect(250, 10, 120, 100)) self.lcdNumber.setDigitCount(2) self.lcdNumber.setObjectName("lcdNumber") self.lcdNumber.setStyleSheet("QLCDNumber {color: orange;}") self.plainTextEdit = self.nameText = QtWidgets.QTextEdit(self.centralwidget) self.plainTextEdit.setGeometry(QtCore.QRect(60, 190, 161, 31)) self.plainTextEdit.setObjectName("plainTextEdit") self.plainTextEdit.setFont(QtGui.QFont('sanserif', 17)) BỘ MÔN ĐIỆN TỬ CÔNG NGHIỆP – Y SINH 91 PHỤ LỤC self.plainTextEdit.setText(str(self.car)) self.lcdNumber_2 = QtWidgets.QLCDNumber(self.centralwidget) self.lcdNumber_2.setGeometry(QtCore.QRect(1210, 10, 120, 100)) self.lcdNumber_2.setDigitCount(2) self.lcdNumber_2.setObjectName("lcdNumber_2") self.lcdNumber_2.setStyleSheet("QLCDNumber {color: orange;}") self.plainTextEdit_2 = QtWidgets.QTextEdit(self.centralwidget) self.plainTextEdit_2.setGeometry(QtCore.QRect(60, 240, 161, 31)) self.plainTextEdit_2.setObjectName("plainTextEdit_2") self.plainTextEdit_2.setFont(QtGui.QFont('sanserif', 17)) self.plainTextEdit_2.setText(str(self.bus)) self.plainTextEdit_3 = QtWidgets.QTextEdit(self.centralwidget) self.plainTextEdit_3.setGeometry(QtCore.QRect(60, 290, 161, 31)) self.plainTextEdit_3.setObjectName("plainTextEdit_3") self.plainTextEdit_3.setFont(QtGui.QFont('sanserif', 17)) self.plainTextEdit_3.setText(str(self.truck)) self.pushButton = QtWidgets.QPushButton(self.centralwidget) self.pushButton.setGeometry(QtCore.QRect(60, 30, 90, 30)) self.pushButton.setObjectName("pushButton") self.pushButton.clicked.connect(self.clickAuto) self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget) self.pushButton_2.setGeometry(QtCore.QRect(60, 80, 90, 30)) self.pushButton_2.setObjectName("pushButton_2") self.pushButton_2.clicked.connect(self.clickDefault) self.pushButton_3 = QtWidgets.QPushButton(self.centralwidget) self.pushButton_3.setGeometry(QtCore.QRect(400, 60, 90, 30)) self.pushButton_3.setObjectName("pushButton_3") self.pushButton_3.clicked.connect(self.clickRun1) self.pushButton_4 = QtWidgets.QPushButton(self.centralwidget) self.pushButton_4.setGeometry(QtCore.QRect(1100, 60, 90, 30)) self.pushButton_4.setObjectName("pushButton_4") self.pushButton_4.clicked.connect(self.clickRun2) self.line = QtWidgets.QFrame(self.centralwidget) self.line.setGeometry(QtCore.QRect(0, 140, 1360, 20)) self.line.setFrameShape(QtWidgets.QFrame.HLine) self.line.setFrameShadow(QtWidgets.QFrame.Sunken) self.line.setObjectName("line") self.line_2 = QtWidgets.QFrame(self.centralwidget) self.line_2.setGeometry(QtCore.QRect(230, 150, 20, 610)) self.line_2.setFrameShape(QtWidgets.QFrame.VLine) self.line_2.setFrameShadow(QtWidgets.QFrame.Sunken) self.line_2.setObjectName("line_2") self.line_3 = QtWidgets.QFrame(self.centralwidget) self.line_3.setGeometry(QtCore.QRect(785, 150, 20, 610)) self.line_3.setFrameShape(QtWidgets.QFrame.VLine) self.line_3.setFrameShadow(QtWidgets.QFrame.Sunken) self.line_3.setObjectName("line_3") self.label = QtWidgets.QLabel(self.centralwidget) self.label.setGeometry(QtCore.QRect(10, 200, 46, 14)) self.label.setObjectName("label") self.label_2 = QtWidgets.QLabel(self.centralwidget) self.label_2.setGeometry(QtCore.QRect(10, 250, 46, 14)) self.label_2.setObjectName("label_2") self.label_3 = QtWidgets.QLabel(self.centralwidget) self.label_3.setGeometry(QtCore.QRect(10, 300, 46, 14)) self.label_3.setObjectName("label_3") self.line_4 = QtWidgets.QFrame(self.centralwidget) self.line_4.setGeometry(QtCore.QRect(0, 340, 241, 45)) self.line_4.setFrameShape(QtWidgets.QFrame.HLine) self.line_4.setFrameShadow(QtWidgets.QFrame.Sunken) self.line_4.setObjectName("line_4") BỘ MÔN ĐIỆN TỬ CÔNG NGHIỆP – Y SINH 92 PHỤ LỤC self.label_4 = QtWidgets.QLabel(self.centralwidget) self.label_4.setGeometry(QtCore.QRect(250, 130, 51, 16)) self.label_4.setObjectName("label_4") self.label_5 = QtWidgets.QLabel(self.centralwidget) self.label_5.setGeometry(QtCore.QRect(1280, 130, 51, 16)) self.label_5.setObjectName("label_5") self.label_6 = QtWidgets.QLabel(self.centralwidget) self.label_6.setGeometry(QtCore.QRect(10, 160, 51, 16)) self.label_6.setObjectName("label_6") MainWindow.setCentralWidget(self.centralwidget) self.statusbar = QtWidgets.QStatusBar(MainWindow) self.statusbar.setObjectName("statusbar") MainWindow.setStatusBar(self.statusbar) self.menubar = QtWidgets.QMenuBar(MainWindow) self.menubar.setGeometry(QtCore.QRect(0, 0, 1084, 21)) self.menubar.setObjectName("menubar") MainWindow.setMenuBar(self.menubar) self.retranslateUi(MainWindow) QtCore.QMetaObject.connectSlotsByName(MainWindow) self.image1 = QtWidgets.QLabel(MainWindow) self.image1.setGeometry(QtCore.QRect(250,160, 530, 530)) self.image1.setPixmap(QtGui.QPixmap('cam1.jpg')) self.image2 = QtWidgets.QLabel(MainWindow) self.image2.setGeometry(QtCore.QRect(810, 160, 530, 530)) self.image2.setPixmap(QtGui.QPixmap('cam1.jpg')) self.image3 = QtWidgets.QLabel(MainWindow) self.image3.setGeometry(QtCore.QRect(10, 375, 220, 220)) self.image3.setPixmap(QtGui.QPixmap('cam1.jpg')) self.image4 = QtWidgets.QLabel(MainWindow) self.image4.setGeometry(QtCore.QRect(900, 20, 100, 100)) self.image4.setPixmap(QtGui.QPixmap('feee_100.jpg')) self.image4 = QtWidgets.QLabel(MainWindow) self.image4.setGeometry(QtCore.QRect(600, 20, 100, 100)) self.image4.setPixmap(QtGui.QPixmap('hcmute_100.jpg')) #Camera self.cap1 = cv2.VideoCapture(2) self.cap2 = cv2.VideoCapture(0) # create a timer self.timer = QtCore.QTimer() self.timer.start(30) self.timer.timeout.connect(self.viewCam) #Serial self.serialPort = MySerialClass() self.serialPort.start() self.recognize() # view camera def viewCam(self): try: # STREET GO if self.serialPort.mess > (self.rtime + self.ytime): if self.serialPort.mess == (self.rtime + self.ytime + self.gtime): BỘ MÔN ĐIỆN TỬ CÔNG NGHIỆP – Y SINH 93 PHỤ LỤC self.lcdNumber.setStyleSheet("QLCDNumber {color: green;}") self.lcdNumber_2.setStyleSheet("QLCDNumber {color: red;}") if not self.auto: self.lcdNumber.display(self.serialPort.mess self.rtime - self.ytime) self.lcdNumber_2.display(self.serialPort.mess self.rtime) if self.serialPort.mess == (self.rtime + self.ytime + 5): if self.default & (not self.auto): frame1 = self.cap1.read()[1] frame1 = frame1[60:,140:440] frame1 = cv2.resize(frame1, (530,530)) cv2.imwrite('recog_image.jpg', frame1) self.recognize() self.image3.setPixmap(QtGui.QPixmap('result_image.jpg')) self.plainTextEdit.setText(str(self.car)) self.plainTextEdit_2.setText(str(self.bus)) self.plainTextEdit_3.setText(str(self.truck)) if (self.car + self.bus + self.truck) >= self.threshold: self.serialPort.send('add') elif self.serialPort.mess > self.rtime: if self.serialPort.mess == (self.rtime + self.ytime): if not self.auto: self.lcdNumber.setStyleSheet("QLCDNumber {color: yellow;}") self.lcdNumber_2.setStyleSheet("QLCDNumber {color: red;}") if not self.auto: self.lcdNumber.display(self.serialPort.mess self.rtime) self.lcdNumber_2.display(self.serialPort.mess self.rtime) #STREET GO elif self.serialPort.mess > self.ytime: if self.serialPort.mess == self.rtime: self.lcdNumber.setStyleSheet("QLCDNumber {color: red;}") self.lcdNumber_2.setStyleSheet("QLCDNumber {color: green;}") if not self.auto: self.lcdNumber.display(self.serialPort.mess) self.lcdNumber_2.display(self.serialPort.mess self.ytime) if self.serialPort.mess == (self.ytime + 5): if self.default & (not self.auto): frame2 = self.cap2.read()[1] frame2 = frame2[60:,140:440] frame2 = cv2.resize(frame2, (530,530)) cv2.imwrite('recog_image.jpg', frame2) self.recognize() self.image3.setPixmap(QtGui.QPixmap('result_image.jpg')) self.plainTextEdit.setText(str(self.car)) self.plainTextEdit_2.setText(str(self.bus)) self.plainTextEdit_3.setText(str(self.truck)) if (self.car + self.bus + self.truck) >= self.threshold: self.serialPort.send('add') elif self.serialPort.mess 0.7: # Object detected center_x = int(detection[0] * width) center_y = int(detection[1] * height) w = int(detection[2] * width) h = int(detection[3] * height) # Rectangle coordinates x = int(center_x - w / 2) y = int(center_y - h / 2) exist = False for (x_bef, y_bef) in zip(x_list, y_list): if (abs(x - x_bef) > 15) or (abs(y - y_bef) > 15): continue else: exist = True break if not exist: x_list.append(x) y_list.append(y) boxes.append([x, y, w, h]) confidences.append(float(confidence)) BỘ MÔN ĐIỆN TỬ CÔNG NGHIỆP – Y SINH 95 PHỤ LỤC class_ids.append(class_id) print(x_list) print(class_ids) indexes = cv2.dnn.NMSBoxes(boxes, confidences, 0.5, 0.4) font = cv2.FONT_HERSHEY_PLAIN for i in range(len(boxes)): if i in indexes: x, y, w, h = boxes[i] label = str(self.classes[class_ids[i]]) color = self.colors[class_ids[i]] cv2.rectangle(img, (x, y), (x + w, y + h), color, 2) cv2.putText(img, label, (x, y ), font, 1, color, 3) cv2.imwrite('result_image_full.jpg', img) img = cv2.resize(img, (220,220)) cv2.imwrite('result_image.jpg', img) self.car = class_ids.count(0) self.bus = class_ids.count(1) self.truck = class_ids.count(2) def clickAuto(self): if self.auto: self.serialPort.send('auto') self.pushButton.setText( "AUTO") self.auto = False self.lcdNumber.setStyleSheet("QLCDNumber {color: green;}") self.lcdNumber_2.setStyleSheet("QLCDNumber {color: red;}") else: self.serialPort.send('manual') self.pushButton.setText("MANUAL") self.auto = True self.lcdNumber.setStyleSheet("QLCDNumber {color: orange;}") self.lcdNumber_2.setStyleSheet("QLCDNumber {color: orange;}") self.lcdNumber.display(0) self.lcdNumber_2.display(0) def clickDefault(self): if self.default: #self.serialPort.send('default') self.pushButton_2.setText("DEFAULT") self.default = False else: #self.serialPort.send('yolo') self.pushButton_2.setText("YOLO") self.default = True def clickRun1(self): if self.auto: self.serialPort.send('run1') self.lcdNumber.setStyleSheet("QLCDNumber {color: green;}") self.lcdNumber_2.setStyleSheet("QLCDNumber {color: red;}") def clickRun2(self): if self.auto: self.serialPort.send('run2') self.lcdNumber.setStyleSheet("QLCDNumber {color: red;}") self.lcdNumber_2.setStyleSheet("QLCDNumber {color: green;}") def retranslateUi(self, MainWindow): _translate = QtCore.QCoreApplication.translate MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow")) self.pushButton.setText(_translate("MainWindow", "MANUAL")) self.pushButton_2.setText(_translate("MainWindow", "DEFAULT")) self.pushButton_3.setText(_translate("MainWindow", "STREET RUN")) self.pushButton_4.setText(_translate("MainWindow", "STREET RUN")) BỘ MÔN ĐIỆN TỬ CÔNG NGHIỆP – Y SINH 96 PHỤ LỤC self.label.setText(_translate("MainWindow", "CAR")) self.label_2.setText(_translate("MainWindow", "BUS")) self.label_3.setText(_translate("MainWindow", "TRUCK")) self.label_4.setText(_translate("MainWindow", "STREET 1")) self.label_5.setText(_translate("MainWindow", "STREET 2")) self.label_6.setText(_translate("MainWindow", "RESULT:")) if name == " main ": import sys app = QtWidgets.QApplication(sys.argv) MainWindow = QtWidgets.QMainWindow() ui = Ui_MainWindow() ui.setupUi(MainWindow) MainWindow.show() sys.exit(app.exec_()) BỘ MÔN ĐIỆN TỬ CÔNG NGHIỆP – Y SINH 97 S K L 0 ... nhóm thực đồ án chọn đề tài: ? ?Ứng dụng xử lý ảnh vào mơ hình điều khiển đèn giao thơng ngã tư? ?? Nhằm góp phần vào việc xây dựng phương pháp điều khiển đèn tín hiệu giao thơng cách hợp lý hơn, giúp... TỬ BỘ MÔN ĐIỆN TỬ CÔNG NGHIỆP – Y SINH - ĐỒ ÁN TỐT NGHIỆP NGÀNH CÔNG NGHỆ KỸ THUẬT ĐIỆN TỬ TRUYỀN THÔNG ĐỀ TÀI: ỨNG DỤNG XỬ LÝ ẢNH VÀO MÔ HÌNH ĐIỀU KHIỂN ĐÈN GIAO THƠNG NGÃ TƯ GVHD:... cổng USB Hình 3.2: Camera kết nối với máy tính 3.2.2.2 Khối xử lý ảnh đầu vào Thiết kế chương trình để xử lý hình ảnh đầu vào cách ứng dụng thuật toán Yolov3 Để đếm đối tư? ??ng hình ảnh đưa vào từ

Ngày đăng: 15/02/2023, 14:31

Tài liệu cùng người dùng

Tài liệu liên quan