{"id":650,"date":"2026-06-11T13:42:30","date_gmt":"2026-06-11T05:42:30","guid":{"rendered":"https:\/\/projectleon.net\/make\/blog\/traffic-density-esp32cam-opencv-vehicle-count-intersection-farsi\/"},"modified":"2026-06-11T13:42:30","modified_gmt":"2026-06-11T05:42:30","slug":"traffic-density-esp32cam-opencv-vehicle-count-intersection-farsi","status":"publish","type":"post","link":"https:\/\/projectleon.net\/make\/blog\/traffic-density-esp32cam-opencv-vehicle-count-intersection-farsi\/","title":{"rendered":"\u0633\u0627\u062e\u062a \u0633\u06cc\u0633\u062a\u0645 \u062a\u0631\u0627\u06a9\u0645 \u062a\u0631\u0627\u0641\u06cc\u06a9 \u0628\u0627 ESP32-CAM \u0648 OpenCV: \u0634\u0645\u0627\u0631\u0634 \u0645\u0627\u0634\u06cc\u0646 \u062f\u0631 \u062a\u0642\u0627\u0637\u0639 (\u0641\u0627\u0631\u0633\u06cc)"},"content":{"rendered":"<div class='hero-image-container' style='margin-bottom:28px;border-radius:14px;overflow:hidden;box-shadow:0 4px 18px rgba(0,0,0,0.08)'>\n<img src='https:\/\/projectleon.net\/make\/wp-content\/uploads\/2026\/06\/post115-traffic-esp32cam.jpg' alt='ESP32-CAM OpenCV \u0634\u0645\u0627\u0631\u0634 \u062a\u0631\u0627\u0641\u06cc\u06a9 \u062a\u0642\u0627\u0637\u0639' style='width:100%;height:auto;display:block' loading='eager'\/>\n<\/div>\n<div class='beginner-intro' style='background:#f0f7ff;border-right:4px solid #2e5fb6;padding:16px 20px;border-radius:10px;margin-bottom:22px'>\n<h2 style='margin-top:0'>\u0633\u06cc\u0633\u062a\u0645 \u0634\u0645\u0627\u0631\u0634 \u062a\u0631\u0627\u0641\u06cc\u06a9 \u0686\u06cc\u0633\u062a\u061f (\u0645\u0628\u062a\u062f\u06cc)<\/h2>\n<p>\u062f\u0631 \u0627\u06cc\u0646 \u067e\u0631\u0648\u0698\u0647\u060c \u06cc\u06a9 \u062f\u0648\u0631\u0628\u06cc\u0646 <strong>ESP32-CAM<\/strong> (\u0627\u0631\u0632\u0627\u0646\u060c WiFi\u060c \u062f\u0648\u0631\u0628\u06cc\u0646 OV2640) \u062a\u0635\u0648\u06cc\u0631 \u062a\u0642\u0627\u0637\u0639 \u0631\u0627 capture \u0645\u06cc\u200c\u06a9\u0646\u062f \u0648 \u0628\u0647 Raspberry Pi \u0627\u0631\u0633\u0627\u0644 \u0645\u06cc\u200c\u06a9\u0646\u062f. \u0631\u0648\u06cc Raspberry Pi\u060c <strong>OpenCV<\/strong> \u0628\u0627 \u0627\u0644\u06af\u0648\u0631\u06cc\u062a\u0645 <strong>MOG2<\/strong> (Mixture of Gaussians Background Subtraction) \u067e\u06cc\u06a9\u0633\u0644\u200c\u0647\u0627\u06cc \u0645\u062a\u062d\u0631\u06a9 \u0631\u0627 \u0634\u0646\u0627\u0633\u0627\u06cc\u06cc \u0645\u06cc\u200c\u06a9\u0646\u062f.<\/p>\n<p>\u0648\u0642\u062a\u06cc \u06cc\u06a9 \u0645\u0627\u0634\u06cc\u0646 \u0627\u0632 \u062e\u0637 \u0634\u0645\u0627\u0631\u0634 \u0639\u0628\u0648\u0631 \u0645\u06cc\u200c\u06a9\u0646\u062f\u060c \u0634\u0645\u0627\u0631\u0646\u062f\u0647 \u06cc\u06a9 \u0648\u0627\u062d\u062f \u0627\u0641\u0632\u0627\u06cc\u0634 \u0645\u06cc\u200c\u06cc\u0627\u0628\u062f. \u0627\u06cc\u0646 \u0633\u06cc\u0633\u062a\u0645 \u0645\u06cc\u200c\u062a\u0648\u0627\u0646\u062f \u0628\u0647 \u0633\u06cc\u0633\u062a\u0645\u200c\u0647\u0627\u06cc \u0647\u0648\u0634\u0645\u0646\u062f \u0645\u062f\u06cc\u0631\u06cc\u062a \u062a\u0631\u0627\u0641\u06cc\u06a9 UGV\/AGV \u062f\u0631 \u0645\u062d\u06cc\u0637\u200c\u0647\u0627\u06cc \u0645\u0633\u062f\u0648\u062f \u06cc\u0627 \u0627\u0646\u0628\u0627\u0631 \u0645\u062a\u0635\u0644 \u0634\u0648\u062f \u2014 \u0645\u062b\u0644\u0627\u064b AGV \u0631\u0627 \u0645\u062a\u0648\u0642\u0641 \u06a9\u0646\u062f \u0648\u0642\u062a\u06cc \u062a\u0631\u0627\u0641\u06cc\u06a9 \u0633\u0646\u06af\u06cc\u0646 \u0627\u0633\u062a.<\/p>\n<\/div>\n<div style='border:1px solid #d8d8d8;padding:18px 20px;border-radius:12px;background:#fafafa;margin-bottom:22px'>\n<h2 style='margin-top:0'>\u0645\u062d\u0635\u0648\u0644\u06cc \u06a9\u0647 \u0645\u06cc\u200c\u0633\u0627\u0632\u06cc\u062f<\/h2>\n<p>\u0633\u06cc\u0633\u062a\u0645 \u0634\u0645\u0627\u0631\u0634 \u0648\u0633\u06cc\u0644\u0647 \u0646\u0642\u0644\u06cc\u0647 \u0628\u0627 \u062f\u0648\u0631\u0628\u06cc\u0646 ESP32-CAM \u0648 OpenCV.<\/p>\n<ul>\n<li><strong>\u0647\u0632\u06cc\u0646\u0647:<\/strong> ESP32-CAM \u2248 \u06f5 \u062f\u0644\u0627\u0631 + RPi<\/li>\n<li><strong>\u062f\u0642\u062a:<\/strong> \u06f8\u06f5-\u06f9\u06f5% \u062f\u0631 \u0646\u0648\u0631 \u0631\u0648\u0632<\/li>\n<li><strong>\u0644\u0627\u06af:<\/strong> CSV \u0628\u0627 timestamp<\/li>\n<\/ul>\n<\/div>\n<p><svg viewBox='0 0 860 180' width='100%' xmlns='http:\/\/www.w3.org\/2000\/svg' style='background:#111;border:1px solid #333;border-radius:12px;margin-bottom:22px'>\n<!-- \u062a\u0635\u0648\u06cc\u0631 \u062f\u0648\u0631\u0628\u06cc\u0646 -->\n<rect x='30' y='20' width='360' height='140' rx='4' fill='#1a2a1a'\/>\n<text x='210' y='18' text-anchor='middle' font-size='10' font-family='monospace' fill='#888'>Frame ESP32-CAM<\/text>\n<!-- \u062c\u0627\u062f\u0647 -->\n<rect x='30' y='80' width='360' height='80' rx='0' fill='#2a2a2a'\/>\n<line x1='30' y1='110' x2='390' y2='110' stroke='#ffff00' stroke-width='1' stroke-dasharray='10,8'\/>\n<!-- \u062e\u0637 \u0634\u0645\u0627\u0631\u0634 -->\n<line x1='200' y1='80' x2='200' y2='160' stroke='#4fc3f7' stroke-width='2' stroke-dasharray='4,2'\/>\n<text x='200' y='75' text-anchor='middle' font-size='9' font-family='monospace' fill='#4fc3f7'>Count Line<\/text>\n<!-- \u0645\u0627\u0634\u06cc\u0646 -->\n<rect x='240' y='88' width='50' height='30' rx='4' fill='#ff5722'\/>\n<text x='265' y='107' text-anchor='middle' font-size='8' font-family='monospace' fill='#fff'>CAR<\/text>\n<!-- \u0634\u0645\u0627\u0631\u0634 -->\n<rect x='420' y='50' width='180' height='80' rx='8' fill='#1a237e'\/>\n<text x='510' y='85' text-anchor='middle' font-size='28' font-family='monospace' fill='#4fc3f7'>47<\/text>\n<text x='510' y='108' text-anchor='middle' font-size='10' font-family='monospace' fill='#aaa'>\u0648\u0633\u06cc\u0644\u0647 \u0627\u0645\u0631\u0648\u0632<\/text>\n<text x='430' y='170' text-anchor='middle' font-size='11' font-family='Tahoma' fill='#888'>OpenCV MOG2 \u2014 \u0634\u0645\u0627\u0631\u0634 \u0639\u0628\u0648\u0631 \u0627\u0632 \u062e\u0637 \u0622\u0628\u06cc<\/text>\n<\/svg><\/p>\n<h2>\u06a9\u062f ESP32-CAM \u2014 \u0633\u0631\u0648\u0631 MJPEG<\/h2>\n<pre style='background:#1e1e1e;color:#d4d4d4;padding:14px;border-radius:10px;overflow-x:auto;font-size:13px'><code class='language-cpp'>\n#include \"esp_camera.h\"\n#include &lt;WiFi.h&gt;\n#include \"esp_http_server.h\"\n\nconst char* ssid = \"YourWiFi\";\nconst char* password = \"YourPass\";\n\n\/\/ Camera config \u0628\u0631\u0627\u06cc AI-Thinker ESP32-CAM:\ncamera_config_t config;\nconfig.ledc_channel = LEDC_CHANNEL_0;\nconfig.pin_d0 = 5; config.pin_d1 = 18; config.pin_d2 = 19;\nconfig.pin_d3 = 21; config.pin_d4 = 36; config.pin_d5 = 39;\nconfig.pin_d6 = 34; config.pin_d7 = 35;\nconfig.pin_xclk = 0; config.pin_pclk = 22;\nconfig.pin_vsync = 25; config.pin_href = 23;\nconfig.pin_sscb_sda = 26; config.pin_sscb_scl = 27;\nconfig.pin_reset = -1; config.xclk_freq_hz = 20000000;\nconfig.pixel_format = PIXFORMAT_JPEG;\nconfig.frame_size = FRAMESIZE_VGA;  \/\/ \u06f6\u06f4\u06f0\u00d7\u06f4\u06f8\u06f0\nconfig.jpeg_quality = 15;\nconfig.fb_count = 2;\n\n\/\/ \u0633\u0631\u0648\u0631 MJPEG \u0633\u0627\u062f\u0647 \u0631\u0627\u0647\u200c\u0627\u0646\u062f\u0627\u0632\u06cc \u0645\u06cc\u200c\u0634\u0648\u062f\n\/\/ \u0631\u0648\u06cc http:\/\/[IP]:80\/stream \u0642\u0627\u0628\u0644 \u062f\u0633\u062a\u0631\u0633\n<\/code><\/pre>\n<h2>\u06a9\u062f Python OpenCV \u2014 \u0634\u0645\u0627\u0631\u0634 \u0645\u0627\u0634\u06cc\u0646<\/h2>\n<pre style='background:#1e1e1e;color:#d4d4d4;padding:14px;border-radius:10px;overflow-x:auto;font-size:13px'><code class='language-python'>\nimport cv2\nimport numpy as np\nimport urllib.request\nimport datetime, csv\n\nESP32_URL = \"http:\/\/192.168.1.105\/stream\"\nCOUNT_LINE_Y = 240   # \u0646\u0635\u0641 \u0627\u0631\u062a\u0641\u0627\u0639 \u06f4\u06f8\u06f0 = \u062e\u0637 \u0634\u0645\u0627\u0631\u0634\n\nfgbg = cv2.createBackgroundSubtractorMOG2(history=100, varThreshold=50)\ncount = 0\nprev_centers = []\nlog_file = open(\"traffic_log.csv\",\"w\",newline='')\nwriter = csv.writer(log_file)\nwriter.writerow([\"time\",\"count\"])\n\ncap = cv2.VideoCapture(ESP32_URL)\n\nwhile True:\n    ret, frame = cap.read()\n    if not ret: continue\n\n    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)\n    fgmask = fgbg.apply(gray)\n\n    # \u062d\u0630\u0641 \u0646\u0648\u06cc\u0632:\n    kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(5,5))\n    fgmask = cv2.morphologyEx(fgmask, cv2.MORPH_CLOSE, kernel)\n\n    # \u067e\u06cc\u062f\u0627 \u06a9\u0631\u062f\u0646 Contour\u0647\u0627:\n    contours, _ = cv2.findContours(fgmask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)\n    for cnt in contours:\n        if cv2.contourArea(cnt) > 1500:\n            x,y,w,h = cv2.boundingRect(cnt)\n            cy = y + h\/\/2\n            if abs(cy - COUNT_LINE_Y) < 10:\n                count += 1\n                writer.writerow([datetime.datetime.now().isoformat(), count])\n                log_file.flush()\n\n    cv2.line(frame, (0,COUNT_LINE_Y), (640,COUNT_LINE_Y), (255,0,0), 2)\n    cv2.putText(frame, f\"Count: {count}\", (10,30),\n                cv2.FONT_HERSHEY_SIMPLEX, 1, (0,255,0), 2)\n    cv2.imshow(\"Traffic Counter\", frame)\n    if cv2.waitKey(1) == ord('q'): break\n\ncap.release()\n<\/code><\/pre>\n<h2>\u0686\u06a9\u200c\u0644\u06cc\u0633\u062a \u0646\u0635\u0628 \u0648 \u062a\u0646\u0638\u06cc\u0645<\/h2>\n<table style='width:100%;border-collapse:collapse;margin-bottom:18px'>\n<thead>\n<tr style='background:#e8f0fe'>\n<th style='padding:9px;border:1px solid #ddd;text-align:right'>\u0628\u0631\u0631\u0633\u06cc<\/th>\n<th style='padding:9px;border:1px solid #ddd;text-align:right'>\u0631\u0648\u0634<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style='padding:8px;border:1px solid #ddd'>ESP32-CAM \u0631\u0648\u06cc WiFi \u0648\u0635\u0644 \u0645\u06cc\u200c\u0634\u0648\u062f\u061f<\/td>\n<td style='padding:8px;border:1px solid #ddd'>Serial Monitor IP \u0631\u0627 \u0646\u0634\u0627\u0646 \u062f\u0647\u062f<\/td>\n<\/tr>\n<tr style='background:#fafafa'>\n<td style='padding:8px;border:1px solid #ddd'>MJPEG Stream \u062f\u0631 \u0645\u0631\u0648\u0631\u06af\u0631 \u0628\u0627\u0632 \u0645\u06cc\u200c\u0634\u0648\u062f\u061f<\/td>\n<td style='padding:8px;border:1px solid #ddd'>http:\/\/[IP]\/stream<\/td>\n<\/tr>\n<tr>\n<td style='padding:8px;border:1px solid #ddd'>MOG2 \u062f\u0631 \u0634\u0628\/\u0631\u0648\u0634\u0646\u0627\u06cc\u06cc \u06a9\u0645 \u06a9\u0627\u0631 \u0645\u06cc\u200c\u06a9\u0646\u062f\u061f<\/td>\n<td style='padding:8px;border:1px solid #ddd'>varThreshold \u0631\u0627 \u06a9\u0645 \u06a9\u0646\u06cc\u062f<\/td>\n<\/tr>\n<tr style='background:#fafafa'>\n<td style='padding:8px;border:1px solid #ddd'>\u0634\u0645\u0627\u0631\u0634 \u062f\u0648\u062a\u0627\u06cc\u06cc (Double Counting) \u0646\u0645\u06cc\u200c\u06a9\u0646\u062f\u061f<\/td>\n<td style='padding:8px;border:1px solid #ddd'>Cooldown Timer \u0627\u0636\u0627\u0641\u0647 \u06a9\u0646\u06cc\u062f<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><svg viewBox='0 0 860 200' width='100%' xmlns='http:\/\/www.w3.org\/2000\/svg' style='background:#fff;border:1px solid #e0e0e0;border-radius:12px;margin-bottom:22px'>\n<rect x='40' y='75' width='155' height='60' rx='12' fill='#f7c6c6' stroke='#a03030' stroke-width='2'\/>\n<rect x='245' y='75' width='155' height='60' rx='12' fill='#f7d794' stroke='#a06d00' stroke-width='2'\/>\n<rect x='450' y='75' width='155' height='60' rx='12' fill='#b7e4a0' stroke='#3a7a1e' stroke-width='2'\/>\n<rect x='655' y='75' width='165' height='60' rx='12' fill='#e8f4ff' stroke='#3a60c0' stroke-width='2'\/>\n<line x1='195' y1='105' x2='245' y2='105' stroke='#555' stroke-width='2.5' stroke-dasharray='6,3'\/>\n<line x1='400' y1='105' x2='450' y2='105' stroke='#555' stroke-width='2.5'\/>\n<line x1='605' y1='105' x2='655' y2='105' stroke='#555' stroke-width='2.5'\/>\n<text x='117' y='100' text-anchor='middle' font-size='12' font-family='Tahoma'>ESP32-CAM<\/text>\n<text x='117' y='115' text-anchor='middle' font-size='11' font-family='Tahoma'>MJPEG WiFi<\/text>\n<text x='322' y='100' text-anchor='middle' font-size='12' font-family='Tahoma'>OpenCV MOG2<\/text>\n<text x='322' y='115' text-anchor='middle' font-size='11' font-family='Tahoma'>Python<\/text>\n<text x='527' y='100' text-anchor='middle' font-size='12' font-family='Tahoma'>\u0634\u0645\u0627\u0631\u0634 \u0639\u0628\u0648\u0631<\/text>\n<text x='527' y='115' text-anchor='middle' font-size='11' font-family='Tahoma'>CSV \u0644\u0627\u06af<\/text>\n<text x='737' y='100' text-anchor='middle' font-size='12' font-family='Tahoma'>Dashboard<\/text>\n<text x='737' y='115' text-anchor='middle' font-size='11' font-family='Tahoma'>\u0646\u0645\u0627\u06cc\u0634 \u0632\u0646\u062f\u0647<\/text>\n<text x='430' y='175' text-anchor='middle' font-size='13' font-family='Tahoma' fill='#666'>ESP32-CAM \u2192 OpenCV MOG2 \u2192 \u0634\u0645\u0627\u0631\u0634 \u0645\u0627\u0634\u06cc\u0646 \u2192 \u0644\u0627\u06af CSV<\/text>\n<\/svg><\/p>\n<h2>\u0648\u06cc\u062f\u0626\u0648\u0647\u0627\u06cc \u0622\u0645\u0648\u0632\u0634\u06cc<\/h2>\n<div style='display:grid;grid-template-columns:repeat(auto-fit,minmax(300px,1fr));gap:18px;margin:20px 0'>\n<iframe width='100%' height='315' src='https:\/\/www.youtube.com\/embed\/MntcTqmKido' title='OpenCV Vehicle Counter Python MOG2' frameborder='0' allow='accelerometer;autoplay;clipboard-write;encrypted-media;gyroscope;picture-in-picture' allowfullscreen><\/iframe><br \/>\n<iframe width='100%' height='315' src='https:\/\/www.youtube.com\/embed\/T6Gf_-GSSRY' title='ESP32 CAM OpenCV Raspberry Pi Vision' frameborder='0' allow='accelerometer;autoplay;clipboard-write;encrypted-media;gyroscope;picture-in-picture' allowfullscreen><\/iframe>\n<\/div>\n<h2>\u0645\u0646\u0627\u0628\u0639 \u0628\u06cc\u0634\u062a\u0631<\/h2>\n<ul>\n<li><a href='https:\/\/www.aparat.com\/result\/OpenCV_%D8%B4%D9%85%D8%A7%D8%B1%D8%B4_%D9%85%D8%A7%D8%B4%DB%8C%D9%86_%D9%81%D8%A7%D8%B1%D8%B3%DB%8C' target='_blank'>\u0622\u067e\u0627\u0631\u0627\u062a: OpenCV \u0634\u0645\u0627\u0631\u0634 \u0645\u0627\u0634\u06cc\u0646 \u0641\u0627\u0631\u0633\u06cc<\/a><\/li>\n<li><a href='https:\/\/randomnerdtutorials.com\/esp32-cam-video-streaming-web-server\/' target='_blank'>Random Nerd: ESP32-CAM Video Streaming<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u0622\u0645\u0648\u0632\u0634 \u0633\u0627\u062e\u062a \u0633\u06cc\u0633\u062a\u0645 \u0647\u0648\u0634\u0645\u0646\u062f \u0634\u0645\u0627\u0631\u0634 \u0648\u0633\u06cc\u0644\u0647 \u062f\u0631 \u062a\u0642\u0627\u0637\u0639 \u0628\u0627 ESP32-CAM \u0648 \u067e\u0631\u062f\u0627\u0632\u0634 OpenCV \u0631\u0648\u06cc Raspberry Pi\u061b \u062a\u0634\u062e\u06cc\u0635 \u062d\u0631\u06a9\u062a MOG2\u060c \u0634\u0645\u0627\u0631\u0634 \u0641\u0631\u06cc\u0645\u060c \u0647\u0634\u062f\u0627\u0631 \u062a\u0631\u0627\u0641\u06cc\u06a9.<\/p>\n","protected":false},"author":1,"featured_media":649,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"pagelayer_contact_templates":[],"_pagelayer_content":"","footnotes":""},"categories":[16,20],"tags":[63,437,349,23,50,436,424,438],"class_list":["post-650","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ugv","category-projects-2","tag-esp32-cam","tag-mog2","tag-opencv","tag-raspberry-pi","tag-50","tag-436","tag-424","tag-438"],"_links":{"self":[{"href":"https:\/\/projectleon.net\/make\/wp-json\/wp\/v2\/posts\/650","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/projectleon.net\/make\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/projectleon.net\/make\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/projectleon.net\/make\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/projectleon.net\/make\/wp-json\/wp\/v2\/comments?post=650"}],"version-history":[{"count":1,"href":"https:\/\/projectleon.net\/make\/wp-json\/wp\/v2\/posts\/650\/revisions"}],"predecessor-version":[{"id":651,"href":"https:\/\/projectleon.net\/make\/wp-json\/wp\/v2\/posts\/650\/revisions\/651"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/projectleon.net\/make\/wp-json\/wp\/v2\/media\/649"}],"wp:attachment":[{"href":"https:\/\/projectleon.net\/make\/wp-json\/wp\/v2\/media?parent=650"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/projectleon.net\/make\/wp-json\/wp\/v2\/categories?post=650"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/projectleon.net\/make\/wp-json\/wp\/v2\/tags?post=650"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}