Created
March 19, 2026 11:14
-
-
Save Xnuvers007/82de4df745afd9b9ff6905cd96ac6cd8 to your computer and use it in GitHub Desktop.
Data Annotation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import cv2 | |
| import numpy as np | |
| import matplotlib.pyplot as plt | |
| import xml.etree.ElementTree as ET | |
| # 1. Fungsi untuk mengubah warna HEX (#RRGGBB) dari CVAT menjadi BGR untuk OpenCV | |
| def hex_to_bgr(hex_color): | |
| hex_color = hex_color.lstrip('#') | |
| r, g, b = tuple(int(hex_color[i:i+2], 16) for i in (0, 2, 4)) | |
| return (b, g, r) # OpenCV pakai format BGR, bukan RGB | |
| # 2. Nama file input | |
| xml_file = 'annotations.xml' | |
| image_file = 'ssan.jpg' | |
| # 3. Baca gambar asli | |
| img = cv2.imread(image_file) | |
| if img is None: | |
| print(f"[!] ERROR: Gambar '{image_file}' tidak ditemukan. Jangan lupa diupload ya!") | |
| else: | |
| print("[*] Memproses XML dan menggambar anotasi...") | |
| # 4. Parsing (Membaca) file XML | |
| tree = ET.parse(xml_file) | |
| root = tree.getroot() | |
| # 5. Ekstrak warna untuk setiap label dari bagian <labels> | |
| label_colors = {} | |
| for label_tag in root.findall('.//label'): | |
| name = label_tag.find('name').text | |
| color_hex = label_tag.find('color').text | |
| label_colors[name] = hex_to_bgr(color_hex) | |
| # 6. Ekstrak koordinat box dan polyline dari bagian <image> | |
| for image_tag in root.findall('.//image'): | |
| # Menggambar Bounding Box (<box>) | |
| for box in image_tag.findall('box'): | |
| label = box.get('label') | |
| # Ambil koordinat dan ubah dari float/string ke integer | |
| xtl = int(float(box.get('xtl'))) # x top left | |
| ytl = int(float(box.get('ytl'))) # y top left | |
| xbr = int(float(box.get('xbr'))) # x bottom right | |
| ybr = int(float(box.get('ybr'))) # y bottom right | |
| # Ambil warna sesuai label, default ke hijau kalau gak ketemu | |
| color = label_colors.get(label, (0, 255, 0)) | |
| # Gambar kotak dengan ketebalan garis 2 | |
| cv2.rectangle(img, (xtl, ytl), (xbr, ybr), color, 2) | |
| # Menggambar Garis (<polyline>) | |
| for poly in image_tag.findall('polyline'): | |
| label = poly.get('label') | |
| points_str = poly.get('points') | |
| color = label_colors.get(label, (0, 0, 255)) | |
| # Parsing string points format "x1,y1;x2,y2;..." | |
| pts = [] | |
| for p in points_str.split(';'): | |
| x, y = p.split(',') | |
| pts.append([int(float(x)), int(float(y))]) | |
| # Konversi list titik menjadi format array NumPy yang diminta OpenCV | |
| pts = np.array(pts, np.int32) | |
| pts = pts.reshape((-1, 1, 2)) | |
| # Gambar polyline (isClosed=False karena ini cuma garis, bukan polygon tertutup) | |
| cv2.polylines(img, [pts], isClosed=False, color=color, thickness=2) | |
| # 7. Konversi BGR ke RGB agar warnanya normal saat ditampilkan di Matplotlib | |
| img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) | |
| # 8. Tampilkan Hasil | |
| plt.figure(figsize=(12, 8)) | |
| plt.imshow(img_rgb) | |
| plt.axis('off') | |
| plt.title("Hasil Visualisasi XML CVAT") | |
| plt.show() | |
| # 9. (Opsional) Simpan hasilnya ke file baru | |
| # cv2.imwrite('hasil_anotasi.jpg', img) | |
| # print("[*] Gambar berhasil disimpan sebagai 'hasil_anotasi.jpg'") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import cv2 | |
| import numpy as np | |
| import matplotlib.pyplot as plt | |
| import xml.etree.ElementTree as ET | |
| # 1. Fungsi untuk mengubah warna HEX dari CVAT ke BGR untuk OpenCV | |
| def hex_to_bgr(hex_color): | |
| hex_color = hex_color.lstrip('#') | |
| r, g, b = tuple(int(hex_color[i:i+2], 16) for i in (0, 2, 4)) | |
| return (b, g, r) | |
| # 2. Nama file input | |
| xml_file = 'annotations.xml' | |
| image_file = 'ssan.jpg' | |
| # 3. Baca gambar asli | |
| img = cv2.imread(image_file) | |
| if img is None: | |
| print(f"[!] ERROR: Gambar '{image_file}' tidak ditemukan. Jangan lupa diupload ya!") | |
| else: | |
| print("[*] Memproses XML dan menggambar Box, Garis, Titik, serta TEKS...") | |
| # Setup Font untuk Teks | |
| font = cv2.FONT_HERSHEY_SIMPLEX | |
| font_scale = 0.3 | |
| font_thickness = 1 | |
| # 4. Parsing file XML | |
| tree = ET.parse(xml_file) | |
| root = tree.getroot() | |
| # 5. Ekstrak warna untuk setiap label | |
| label_colors = {} | |
| for label_tag in root.findall('.//label'): | |
| name = label_tag.find('name').text | |
| color_hex = label_tag.find('color').text | |
| label_colors[name] = hex_to_bgr(color_hex) | |
| # 6. Ekstrak koordinat dari bagian <image> | |
| for image_tag in root.findall('.//image'): | |
| # --- A. BOUNDING BOX (<box>) + TEKS --- | |
| for box in image_tag.findall('box'): | |
| label = box.get('label') | |
| xtl = int(float(box.get('xtl'))) | |
| ytl = int(float(box.get('ytl'))) | |
| xbr = int(float(box.get('xbr'))) | |
| ybr = int(float(box.get('ybr'))) | |
| color = label_colors.get(label, (0, 255, 0)) | |
| # Gambar Kotak | |
| cv2.rectangle(img, (xtl, ytl), (xbr, ybr), color, 2) | |
| # Tulis Teks Nama Label (di atas kiri kotak) | |
| # ytl - 5 artinya teksnya ditaruh 5 piksel di atas garis kotak | |
| cv2.putText(img, label, (xtl, ytl - 5), font, font_scale, color, font_thickness) | |
| # --- B. GARIS OKLUSI (<polyline>) + TEKS --- | |
| for poly in image_tag.findall('polyline'): | |
| label = poly.get('label') | |
| points_str = poly.get('points') | |
| color = label_colors.get(label, (0, 0, 255)) | |
| pts = [] | |
| for p in points_str.split(';'): | |
| x, y = p.split(',') | |
| pts.append([int(float(x)), int(float(y))]) | |
| pts_array = np.array(pts, np.int32).reshape((-1, 1, 2)) | |
| # Gambar Garis | |
| cv2.polylines(img, [pts_array], isClosed=False, color=color, thickness=2) | |
| # Tulis Teks Nama Label (ditaruh di titik paling awal dari garis) | |
| start_x, start_y = pts[0][0], pts[0][1] | |
| cv2.putText(img, label, (start_x, start_y - 10), font, font_scale, color, font_thickness) | |
| # --- C. TITIK SOLID (<ellipse>) + TEKS --- | |
| for ellipse in image_tag.findall('ellipse'): | |
| label = ellipse.get('label') | |
| cx = int(float(ellipse.get('cx'))) | |
| cy = int(float(ellipse.get('cy'))) | |
| rx = int(float(ellipse.get('rx'))) | |
| ry = int(float(ellipse.get('ry'))) | |
| color = label_colors.get(label, (255, 0, 0)) | |
| # Gambar Titik/Elips Penuh | |
| cv2.ellipse(img, (cx, cy), (rx, ry), 0, 0, 360, color, -1) | |
| # Tulis Teks Nama Label (ditaruh agak ke kanan dari titik biar gak nutupin titiknya) | |
| cv2.putText(img, label, (cx + 10, cy + 5), font, font_scale, color, font_thickness) | |
| # 7. Konversi BGR ke RGB untuk Matplotlib | |
| img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) | |
| # 8. Tampilkan Hasil di Layar Colab | |
| plt.figure(figsize=(14, 10)) | |
| plt.imshow(img_rgb) | |
| plt.axis('off') | |
| plt.title("Hasil Visualisasi Box, Garis, Titik, dan TEKS LABEL") | |
| plt.show() | |
| # 9. Simpan hasilnya ke file baru | |
| cv2.imwrite('hasil_anotasi_dengan_teks.jpg', img) | |
| print("[*] Gambar berhasil disimpan sebagai 'hasil_anotasi_dengan_teks.jpg'") |
Author
Xnuvers007
commented
Mar 19, 2026
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment