چگونه یک اسکنر بارکد پایتون برای ویندوز، لینوکس و macOS بسازیم

Summarize this content to 400 words in Persian Lang
اسکن بارکد به یک ابزار ضروری در صنایع مختلف، از خرده فروشی و لجستیک گرفته تا مراقبت های بهداشتی تبدیل شده است. در پلتفرمهای دسکتاپ، ضبط و پردازش سریع اطلاعات را بدون وارد کردن دستی دادهها، صرفهجویی در زمان و کاهش خطاها امکانپذیر میسازد. در این آموزش، ما به بررسی قابلیت های آن می پردازیم Dynamsoft Capture Vision SDK با ساختن الف بارکد اسکنر پایتون برای ویندوز، لینوکس، و macOS.
نسخه ی نمایشی اسکنر بارکد پایتون در macOS
پیش نیازها
مجوز آزمایشی Dynamsoft Capture Vision: یک کلید مجوز آزمایشی 30 روزه برای Dynamsoft Capture Vision SDK دریافت کنید.
بسته های پایتون: بسته های پایتون مورد نیاز را با استفاده از دستورات زیر نصب کنید:
pip install dynamsoft-capture-vision-bundle opencv-python
این بسته ها برای چیست؟
dynamsoft-capture-vision-bundle Dynamsoft Capture Vision SDK برای پایتون است.
opencv-python فریم های دوربین را می گیرد و نتایج تصویر پردازش شده را نمایش می دهد.
خواندن بارکد از تصاویر استاتیک
از آنجایی که Dynamsoft Capture Vision SDK یک چارچوب یکپارچه است که با وظایف مختلف پردازش تصویر یکپارچه شده است، میتوانیم به راحتی بین حالتهای پردازش تصویر جابجا شویم. Preset Template نام به capture() روش
الگوهای داخلی Dynamsoft Capture Vision SDK
قطعه کد زیر داخلی را نشان می دهد Preset Template شمارش در Dynamsoft Capture Vision SDK:
class EnumPresetTemplate(Enum):
PT_DEFAULT = _DynamsoftCaptureVisionRouter.getPT_DEFAULT()
PT_READ_BARCODES = _DynamsoftCaptureVisionRouter.getPT_READ_BARCODES()
PT_RECOGNIZE_TEXT_LINES = _DynamsoftCaptureVisionRouter.getPT_RECOGNIZE_TEXT_LINES()
PT_DETECT_DOCUMENT_BOUNDARIES = (
_DynamsoftCaptureVisionRouter.getPT_DETECT_DOCUMENT_BOUNDARIES()
)
PT_DETECT_AND_NORMALIZE_DOCUMENT = (
_DynamsoftCaptureVisionRouter.getPT_DETECT_AND_NORMALIZE_DOCUMENT()
)
PT_NORMALIZE_DOCUMENT = _DynamsoftCaptureVisionRouter.getPT_NORMALIZE_DOCUMENT()
PT_READ_BARCODES_SPEED_FIRST = (
_DynamsoftCaptureVisionRouter.getPT_READ_BARCODES_SPEED_FIRST()
)
PT_READ_BARCODES_READ_RATE_FIRST = (
_DynamsoftCaptureVisionRouter.getPT_READ_BARCODES_READ_RATE_FIRST()
)
PT_READ_SINGLE_BARCODE = _DynamsoftCaptureVisionRouter.getPT_READ_SINGLE_BARCODE()
PT_RECOGNIZE_NUMBERS = _DynamsoftCaptureVisionRouter.getPT_RECOGNIZE_NUMBERS()
PT_RECOGNIZE_LETTERS = _DynamsoftCaptureVisionRouter.getPT_RECOGNIZE_LETTERS()
PT_RECOGNIZE_NUMBERS_AND_LETTERS = (
_DynamsoftCaptureVisionRouter.getPT_RECOGNIZE_NUMBERS_AND_LETTERS()
)
PT_RECOGNIZE_NUMBERS_AND_UPPERCASE_LETTERS = (
_DynamsoftCaptureVisionRouter.getPT_RECOGNIZE_NUMBERS_AND_UPPERCASE_LETTERS()
)
PT_RECOGNIZE_UPPERCASE_LETTERS = (
_DynamsoftCaptureVisionRouter.getPT_RECOGNIZE_UPPERCASE_LETTERS()
)
وارد حالت تمام صفحه شوید
از حالت تمام صفحه خارج شوید
این PT_DEFAULT الگو از چندین کار، از جمله تشخیص سند، تشخیص MRZ و تشخیص بارکد پشتیبانی می کند. برای بهینه سازی عملکرد به طور خاص برای تشخیص بارکد، الگو را روی آن تنظیم کنید EnumPresetTemplate.PT_READ_BARCODES.value.
کد پایتون برای تشخیص بارکد
با ارجاع به نمونههای تشخیص سند قبلی و شناسایی MRZ، کد زیر را میتوان برای خواندن بارکد از تصاویر ثابت استفاده کرد:
import sys
from dynamsoft_capture_vision_bundle import *
import os
import cv2
import numpy as np
from utils import *
if __name__ == ‘__main__’:
print(“**********************************************************”)
print(“Welcome to Dynamsoft Capture Vision – Barcode Sample”)
print(“**********************************************************”)
error_code, error_message = LicenseManager.init_license(
“LICENSE-KEY”)
if error_code != EnumErrorCode.EC_OK and error_code != EnumErrorCode.EC_LICENSE_CACHE_USED:
print(“License initialization failed: ErrorCode:”,
error_code, “, ErrorString:”, error_message)
else:
cvr_instance = CaptureVisionRouter()
while (True):
image_path = input(
“>> Input your image full path:\n”
“>> ‘Enter’ for sample image or ‘Q’/’q’ to quit\n”
).strip(‘\'”‘)
if image_path.lower() == “q”:
sys.exit(0)
if image_path == “”:
image_path = “../../../images/multi.png”
if not os.path.exists(image_path):
print(“The image path does not exist.”)
continue
result = cvr_instance.capture(
image_path, EnumPresetTemplate.PT_READ_BARCODES.value)
if result.get_error_code() != EnumErrorCode.EC_OK:
print(“Error:”, result.get_error_code(),
result.get_error_string())
else:
cv_image = cv2.imread(image_path)
items = result.get_items()
print(‘Found {} barcodes.’.format(len(items)))
for item in items:
format_type = item.get_format()
text = item.get_text()
print(“Barcode Format:”, format_type)
print(“Barcode Text:”, text)
location = item.get_location()
x1 = location.points[0].x
y1 = location.points[0].y
x2 = location.points[1].x
y2 = location.points[1].y
x3 = location.points[2].x
y3 = location.points[2].y
x4 = location.points[3].x
y4 = location.points[3].y
del location
cv2.drawContours(
cv_image, [np.intp([(x1, y1), (x2, y2), (x3, y3), (x4, y4)])], 0, (0, 255, 0), 2)
cv2.putText(cv_image, text, (x1, y1 – 10),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)
cv2.imshow(
“Original Image with Detected Barcodes”, cv_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
input(“Press Enter to quit…”)
وارد حالت تمام صفحه شوید
از حالت تمام صفحه خارج شوید
توجه: جایگزین کنید LICENSE-KEY با کلید مجوز معتبر شما
آزمایش بارکد خوان پایتون با یک تصویر چند بارکد
رمزگشایی چندین بارکد از یک تصویر یک مورد رایج در خرده فروشی و تدارکات است. تصویر زیر حاوی چندین بارکد با فرمت های مختلف است:
تشخیص همزمان چند بارکد با وب کم
هنگام خواندن بارکد از یک فایل تصویری، ما را فراخوانی می کنیم capture() روش در تاپیک اصلی با این حال، برای پردازش جریانهای ویدیویی بلادرنگ از یک وبکم، رویکرد متفاوتی برای جلوگیری از مسدود کردن رشته اصلی مورد نیاز است. Dynamsoft Capture Vision SDK مکانیزم داخلی را برای مدیریت فریمهای ویدیویی بلادرنگ و پردازش آنها بهصورت ناهمزمان بر روی یک thread کارگر C++ فراهم میکند. برای پیاده سازی این، تمدید کنید ImageSourceAdapter و CapturedResultReceiver کلاسها به ترتیب دادههای تصویر و نتایج گرفته شده را مدیریت میکنند، سپس آن را فراخوانی کنید start_capturing() روشی برای شروع پردازش جریان ویدئو.
from dynamsoft_capture_vision_bundle import *
import cv2
import numpy as np
import queue
from utils import *
class FrameFetcher(ImageSourceAdapter):
def has_next_image_to_fetch(self) -> bool:
return True
def add_frame(self, imageData):
self.add_image_to_buffer(imageData)
class MyCapturedResultReceiver(CapturedResultReceiver):
def __init__(self, result_queue):
super().__init__()
self.result_queue = result_queue
def on_captured_result_received(self, captured_result):
self.result_queue.put(captured_result)
if __name__ == ‘__main__’:
errorCode, errorMsg = LicenseManager.init_license(
“LICENSE-KEY”)
if errorCode != EnumErrorCode.EC_OK and errorCode != EnumErrorCode.EC_LICENSE_CACHE_USED:
print(“License initialization failed: ErrorCode:”,
errorCode, “, ErrorString:”, errorMsg)
else:
vc = cv2.VideoCapture(0)
if not vc.isOpened():
print(“Error: Camera is not opened!”)
exit(1)
cvr = CaptureVisionRouter()
fetcher = FrameFetcher()
cvr.set_input(fetcher)
# Create a thread-safe queue to store captured items
result_queue = queue.Queue()
receiver = MyCapturedResultReceiver(result_queue)
cvr.add_result_receiver(receiver)
errorCode, errorMsg = cvr.start_capturing(
EnumPresetTemplate.PT_READ_BARCODES.value)
if errorCode != EnumErrorCode.EC_OK:
print(“error:”, errorMsg)
while True:
ret, frame = vc.read()
if not ret:
print(“Error: Cannot read frame!”)
break
fetcher.add_frame(convertMat2ImageData(frame))
if not result_queue.empty():
captured_result = result_queue.get_nowait()
items = captured_result.get_items()
for item in items:
if item.get_type() == EnumCapturedResultItemType.CRIT_BARCODE:
text = item.get_text()
location = item.get_location()
x1 = location.points[0].x
y1 = location.points[0].y
x2 = location.points[1].x
y2 = location.points[1].y
x3 = location.points[2].x
y3 = location.points[2].y
x4 = location.points[3].x
y4 = location.points[3].y
cv2.drawContours(
frame, [np.intp([(x1, y1), (x2, y2), (x3, y3), (x4, y4)])], 0, (0, 255, 0), 2)
cv2.putText(frame, text, (x1, y1),
cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
del location
if cv2.waitKey(1) & 0xFF == ord(‘q’):
break
cv2.imshow(‘frame’, frame)
cvr.stop_capturing()
vc.release()
cv2.destroyAllWindows()
وارد حالت تمام صفحه شوید
از حالت تمام صفحه خارج شوید
توضیح
این FrameFetcher کلاس را پیاده سازی می کند ImageSourceAdapter رابط برای تغذیه داده های فریم به بافر داخلی.
این MyCapturedResultReceiver کلاس را پیاده سازی می کند CapturedResultReceiver رابط کاربری این on_captured_result_received متد روی یک رشته کارگر بومی C++ اجرا می شود و ارسال می کند CapturedResult اشیاء به thread اصلی جایی که در یک صف thread-safe برای استفاده بیشتر ذخیره می شوند.
الف CapturedResult شامل چندین CapturedResultItem اشیاء این CRIT_BARCODE نوع نشان دهنده داده های بارکد شناخته شده است.
تست اسکنر بارکد پایتون در macOS
کد منبع
https://github.com/yushulx/python-barcode-qrcode-sdk/tree/main/examples/official/10.x
اسکن بارکد به یک ابزار ضروری در صنایع مختلف، از خرده فروشی و لجستیک گرفته تا مراقبت های بهداشتی تبدیل شده است. در پلتفرمهای دسکتاپ، ضبط و پردازش سریع اطلاعات را بدون وارد کردن دستی دادهها، صرفهجویی در زمان و کاهش خطاها امکانپذیر میسازد. در این آموزش، ما به بررسی قابلیت های آن می پردازیم Dynamsoft Capture Vision SDK با ساختن الف بارکد اسکنر پایتون برای ویندوز، لینوکس، و macOS.
نسخه ی نمایشی اسکنر بارکد پایتون در macOS
پیش نیازها
-
مجوز آزمایشی Dynamsoft Capture Vision: یک کلید مجوز آزمایشی 30 روزه برای Dynamsoft Capture Vision SDK دریافت کنید.
-
بسته های پایتون: بسته های پایتون مورد نیاز را با استفاده از دستورات زیر نصب کنید:
pip install dynamsoft-capture-vision-bundle opencv-python
این بسته ها برای چیست؟
-
dynamsoft-capture-vision-bundle
Dynamsoft Capture Vision SDK برای پایتون است. -
opencv-python
فریم های دوربین را می گیرد و نتایج تصویر پردازش شده را نمایش می دهد.
-
خواندن بارکد از تصاویر استاتیک
از آنجایی که Dynamsoft Capture Vision SDK یک چارچوب یکپارچه است که با وظایف مختلف پردازش تصویر یکپارچه شده است، میتوانیم به راحتی بین حالتهای پردازش تصویر جابجا شویم. Preset Template نام به capture()
روش
الگوهای داخلی Dynamsoft Capture Vision SDK
قطعه کد زیر داخلی را نشان می دهد Preset Template شمارش در Dynamsoft Capture Vision SDK:
class EnumPresetTemplate(Enum):
PT_DEFAULT = _DynamsoftCaptureVisionRouter.getPT_DEFAULT()
PT_READ_BARCODES = _DynamsoftCaptureVisionRouter.getPT_READ_BARCODES()
PT_RECOGNIZE_TEXT_LINES = _DynamsoftCaptureVisionRouter.getPT_RECOGNIZE_TEXT_LINES()
PT_DETECT_DOCUMENT_BOUNDARIES = (
_DynamsoftCaptureVisionRouter.getPT_DETECT_DOCUMENT_BOUNDARIES()
)
PT_DETECT_AND_NORMALIZE_DOCUMENT = (
_DynamsoftCaptureVisionRouter.getPT_DETECT_AND_NORMALIZE_DOCUMENT()
)
PT_NORMALIZE_DOCUMENT = _DynamsoftCaptureVisionRouter.getPT_NORMALIZE_DOCUMENT()
PT_READ_BARCODES_SPEED_FIRST = (
_DynamsoftCaptureVisionRouter.getPT_READ_BARCODES_SPEED_FIRST()
)
PT_READ_BARCODES_READ_RATE_FIRST = (
_DynamsoftCaptureVisionRouter.getPT_READ_BARCODES_READ_RATE_FIRST()
)
PT_READ_SINGLE_BARCODE = _DynamsoftCaptureVisionRouter.getPT_READ_SINGLE_BARCODE()
PT_RECOGNIZE_NUMBERS = _DynamsoftCaptureVisionRouter.getPT_RECOGNIZE_NUMBERS()
PT_RECOGNIZE_LETTERS = _DynamsoftCaptureVisionRouter.getPT_RECOGNIZE_LETTERS()
PT_RECOGNIZE_NUMBERS_AND_LETTERS = (
_DynamsoftCaptureVisionRouter.getPT_RECOGNIZE_NUMBERS_AND_LETTERS()
)
PT_RECOGNIZE_NUMBERS_AND_UPPERCASE_LETTERS = (
_DynamsoftCaptureVisionRouter.getPT_RECOGNIZE_NUMBERS_AND_UPPERCASE_LETTERS()
)
PT_RECOGNIZE_UPPERCASE_LETTERS = (
_DynamsoftCaptureVisionRouter.getPT_RECOGNIZE_UPPERCASE_LETTERS()
)
این PT_DEFAULT
الگو از چندین کار، از جمله تشخیص سند، تشخیص MRZ و تشخیص بارکد پشتیبانی می کند. برای بهینه سازی عملکرد به طور خاص برای تشخیص بارکد، الگو را روی آن تنظیم کنید EnumPresetTemplate.PT_READ_BARCODES.value
.
کد پایتون برای تشخیص بارکد
با ارجاع به نمونههای تشخیص سند قبلی و شناسایی MRZ، کد زیر را میتوان برای خواندن بارکد از تصاویر ثابت استفاده کرد:
import sys
from dynamsoft_capture_vision_bundle import *
import os
import cv2
import numpy as np
from utils import *
if __name__ == '__main__':
print("**********************************************************")
print("Welcome to Dynamsoft Capture Vision - Barcode Sample")
print("**********************************************************")
error_code, error_message = LicenseManager.init_license(
"LICENSE-KEY")
if error_code != EnumErrorCode.EC_OK and error_code != EnumErrorCode.EC_LICENSE_CACHE_USED:
print("License initialization failed: ErrorCode:",
error_code, ", ErrorString:", error_message)
else:
cvr_instance = CaptureVisionRouter()
while (True):
image_path = input(
">> Input your image full path:\n"
">> 'Enter' for sample image or 'Q'/'q' to quit\n"
).strip('\'"')
if image_path.lower() == "q":
sys.exit(0)
if image_path == "":
image_path = "../../../images/multi.png"
if not os.path.exists(image_path):
print("The image path does not exist.")
continue
result = cvr_instance.capture(
image_path, EnumPresetTemplate.PT_READ_BARCODES.value)
if result.get_error_code() != EnumErrorCode.EC_OK:
print("Error:", result.get_error_code(),
result.get_error_string())
else:
cv_image = cv2.imread(image_path)
items = result.get_items()
print('Found {} barcodes.'.format(len(items)))
for item in items:
format_type = item.get_format()
text = item.get_text()
print("Barcode Format:", format_type)
print("Barcode Text:", text)
location = item.get_location()
x1 = location.points[0].x
y1 = location.points[0].y
x2 = location.points[1].x
y2 = location.points[1].y
x3 = location.points[2].x
y3 = location.points[2].y
x4 = location.points[3].x
y4 = location.points[3].y
del location
cv2.drawContours(
cv_image, [np.intp([(x1, y1), (x2, y2), (x3, y3), (x4, y4)])], 0, (0, 255, 0), 2)
cv2.putText(cv_image, text, (x1, y1 - 10),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)
cv2.imshow(
"Original Image with Detected Barcodes", cv_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
input("Press Enter to quit...")
توجه: جایگزین کنید
LICENSE-KEY
با کلید مجوز معتبر شما
آزمایش بارکد خوان پایتون با یک تصویر چند بارکد
رمزگشایی چندین بارکد از یک تصویر یک مورد رایج در خرده فروشی و تدارکات است. تصویر زیر حاوی چندین بارکد با فرمت های مختلف است:
تشخیص همزمان چند بارکد با وب کم
هنگام خواندن بارکد از یک فایل تصویری، ما را فراخوانی می کنیم capture()
روش در تاپیک اصلی با این حال، برای پردازش جریانهای ویدیویی بلادرنگ از یک وبکم، رویکرد متفاوتی برای جلوگیری از مسدود کردن رشته اصلی مورد نیاز است. Dynamsoft Capture Vision SDK مکانیزم داخلی را برای مدیریت فریمهای ویدیویی بلادرنگ و پردازش آنها بهصورت ناهمزمان بر روی یک thread کارگر C++ فراهم میکند. برای پیاده سازی این، تمدید کنید ImageSourceAdapter
و CapturedResultReceiver
کلاسها به ترتیب دادههای تصویر و نتایج گرفته شده را مدیریت میکنند، سپس آن را فراخوانی کنید start_capturing()
روشی برای شروع پردازش جریان ویدئو.
from dynamsoft_capture_vision_bundle import *
import cv2
import numpy as np
import queue
from utils import *
class FrameFetcher(ImageSourceAdapter):
def has_next_image_to_fetch(self) -> bool:
return True
def add_frame(self, imageData):
self.add_image_to_buffer(imageData)
class MyCapturedResultReceiver(CapturedResultReceiver):
def __init__(self, result_queue):
super().__init__()
self.result_queue = result_queue
def on_captured_result_received(self, captured_result):
self.result_queue.put(captured_result)
if __name__ == '__main__':
errorCode, errorMsg = LicenseManager.init_license(
"LICENSE-KEY")
if errorCode != EnumErrorCode.EC_OK and errorCode != EnumErrorCode.EC_LICENSE_CACHE_USED:
print("License initialization failed: ErrorCode:",
errorCode, ", ErrorString:", errorMsg)
else:
vc = cv2.VideoCapture(0)
if not vc.isOpened():
print("Error: Camera is not opened!")
exit(1)
cvr = CaptureVisionRouter()
fetcher = FrameFetcher()
cvr.set_input(fetcher)
# Create a thread-safe queue to store captured items
result_queue = queue.Queue()
receiver = MyCapturedResultReceiver(result_queue)
cvr.add_result_receiver(receiver)
errorCode, errorMsg = cvr.start_capturing(
EnumPresetTemplate.PT_READ_BARCODES.value)
if errorCode != EnumErrorCode.EC_OK:
print("error:", errorMsg)
while True:
ret, frame = vc.read()
if not ret:
print("Error: Cannot read frame!")
break
fetcher.add_frame(convertMat2ImageData(frame))
if not result_queue.empty():
captured_result = result_queue.get_nowait()
items = captured_result.get_items()
for item in items:
if item.get_type() == EnumCapturedResultItemType.CRIT_BARCODE:
text = item.get_text()
location = item.get_location()
x1 = location.points[0].x
y1 = location.points[0].y
x2 = location.points[1].x
y2 = location.points[1].y
x3 = location.points[2].x
y3 = location.points[2].y
x4 = location.points[3].x
y4 = location.points[3].y
cv2.drawContours(
frame, [np.intp([(x1, y1), (x2, y2), (x3, y3), (x4, y4)])], 0, (0, 255, 0), 2)
cv2.putText(frame, text, (x1, y1),
cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
del location
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cv2.imshow('frame', frame)
cvr.stop_capturing()
vc.release()
cv2.destroyAllWindows()
توضیح
- این
FrameFetcher
کلاس را پیاده سازی می کندImageSourceAdapter
رابط برای تغذیه داده های فریم به بافر داخلی. - این
MyCapturedResultReceiver
کلاس را پیاده سازی می کندCapturedResultReceiver
رابط کاربری اینon_captured_result_received
متد روی یک رشته کارگر بومی C++ اجرا می شود و ارسال می کندCapturedResult
اشیاء به thread اصلی جایی که در یک صف thread-safe برای استفاده بیشتر ذخیره می شوند. - الف
CapturedResult
شامل چندینCapturedResultItem
اشیاء اینCRIT_BARCODE
نوع نشان دهنده داده های بارکد شناخته شده است.
تست اسکنر بارکد پایتون در macOS
کد منبع
https://github.com/yushulx/python-barcode-qrcode-sdk/tree/main/examples/official/10.x