Skip to content

Commit 0227f22

Browse files
🐞 fix(notebooks): migrate anomalib from 1.* to 2.* (#390)
* Refactor requirements.txt to simplify anomalib dependency specification * Update Jupyter notebook for model training with improved performance metrics and visualization enhancements - Adjusted download speeds for datasets in output logs. - Updated output structure for predictions to include additional fields. - Changed image dimensions for training and visualization outputs. - Refactored visualization code to allow for customizable overlays and text size. - Updated notebook metadata to reflect the new Python version. * Refactor Jupyter notebook for robotic arm inference - Improved code readability by restructuring print statements for better formatting. - Updated notebook metadata to reflect the new Python version. - Enhanced code organization by adding line breaks for clarity in function calls. * Update requirements.txt * Update requirements.txt * Update requirements.txt * Update requirements.txt * Update requirements.txt --------- Co-authored-by: Adrian Boguszewski <[email protected]>
1 parent 46f53d6 commit 0227f22

File tree

3 files changed

+107
-182
lines changed

3 files changed

+107
-182
lines changed

ai_ref_kits/defect_detection_anomalib/501a_training_a_model_with_cubes_from_a_robotic_arm.ipynb

Lines changed: 60 additions & 146 deletions
Large diffs are not rendered by default.

ai_ref_kits/defect_detection_anomalib/501b_inference_with_a_robotic_arm.ipynb

Lines changed: 44 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -155,12 +155,13 @@
155155
"\n",
156156
" inferencer = OpenVINOInferencer(\n",
157157
" path=openvino_model_path, # Path to the OpenVINO IR model.\n",
158-
" metadata=metadata_path, # Path to the metadata file.\n",
159158
" device=\"CPU\", # We would like to run it on an Intel CPU.\n",
160159
" )\n",
161160
"\n",
162161
" if dataset_path.exists() is False:\n",
163-
" print(\"Make sure you have the dataset in a proper folder or it i already created\")\n",
162+
" print(\n",
163+
" \"Make sure you have the dataset in a proper folder or it i already created\"\n",
164+
" )\n",
164165
"else:\n",
165166
" dataset_path.mkdir(parents=True, exist_ok=True)"
166167
]
@@ -270,22 +271,22 @@
270271
" # Dobot/general imports\n",
271272
" # pylint: disable=wrong-import-order\n",
272273
" import DobotDllType as dType\n",
273-
" \n",
274+
"\n",
274275
" CON_STR = {\n",
275276
" dType.DobotConnect.DobotConnect_NoError: \"DobotConnect_NoError\",\n",
276277
" dType.DobotConnect.DobotConnect_NotFound: \"DobotConnect_NotFound\",\n",
277278
" dType.DobotConnect.DobotConnect_Occupied: \"DobotConnect_Occupied\",\n",
278279
" }\n",
279-
" \n",
280+
"\n",
280281
" # Load Dll and get the CDLL object\n",
281282
" api = dType.load()\n",
282-
" \n",
283+
"\n",
283284
" # Connect Dobot\n",
284285
" state = dType.ConnectDobot(api, \"\", 115200)[0]\n",
285286
" print(\"Connect status:\", CON_STR[state])\n",
286-
" \n",
287+
"\n",
287288
" use_popup = True # True\n",
288-
" \n",
289+
"\n",
289290
" if state == dType.DobotConnect.DobotConnect_NoError:\n",
290291
" print(\n",
291292
" \"[HOME] Restore to home position at first launch, please wait 30 seconds after turnning on the Dobot Magician.\",\n",
@@ -295,14 +296,18 @@
295296
" \" the same side where the pick and place arm is.\",\n",
296297
" )\n",
297298
" print(\"[PLACING BLOCKS] Place the blocks by 3x3.\")\n",
298-
" print(\"[CALIBRATION POINT] Looking from the back of Dobot, the top left block is the calibration point.\")\n",
299-
" print(\"[CALIBRATION] Set the first variable to 0 to test the calibration point, then set 1 to start running.\")\n",
299+
" print(\n",
300+
" \"[CALIBRATION POINT] Looking from the back of Dobot, the top left block is the calibration point.\"\n",
301+
" )\n",
302+
" print(\n",
303+
" \"[CALIBRATION] Set the first variable to 0 to test the calibration point, then set 1 to start running.\"\n",
304+
" )\n",
300305
" print(\n",
301306
" \"[DIRECTION] Standing behind Dobot Magician facing its front direction, X is front and back direction, \"\n",
302307
" \"Y is left and right direction. \",\n",
303308
" )\n",
304309
" print(\"[CONNECTION] Motor of the conveyor belt connects to port Stepper1.\")\n",
305-
" \n",
310+
"\n",
306311
" Calibration__0__Run__1 = 1\n",
307312
" Calibration_X = 221.2288\n",
308313
" Calibration_Y = -117.0036\n",
@@ -325,40 +330,54 @@
325330
" MM_PER_CRICLE = 3.1415926535898 * 36.0\n",
326331
" vel = float(0) * STEP_PER_CRICLE / MM_PER_CRICLE\n",
327332
" dType.SetEMotorEx(api, 1, 0, int(vel), 1)\n",
328-
" \n",
333+
"\n",
329334
" if Calibration__0__Run__1:\n",
330335
" for _ in range(9):\n",
331336
" # initializing and starting multi-threaded webcam input stream\n",
332337
" cam_stream = CameraStream(stream_id=0) # 0 id for main camera\n",
333338
" cam_stream.start()\n",
334-
" \n",
335-
" dType.SetPTPCmdEx(api, 0, (Calibration_X - j), (Calibration_Y - k), (Calibration_Z - 10), 0, 1)\n",
339+
"\n",
340+
" dType.SetPTPCmdEx(\n",
341+
" api,\n",
342+
" 0,\n",
343+
" (Calibration_X - j),\n",
344+
" (Calibration_Y - k),\n",
345+
" (Calibration_Z - 10),\n",
346+
" 0,\n",
347+
" 1,\n",
348+
" )\n",
336349
" dType.SetEndEffectorSuctionCupEx(api, 1, 1)\n",
337-
" dType.SetPTPCmdEx(api, 0, (Place_X - 0), (Place_Y - 0), (Place_Z + 90), 0, 1)\n",
338-
" \n",
350+
" dType.SetPTPCmdEx(\n",
351+
" api, 0, (Place_X - 0), (Place_Y - 0), (Place_Z + 90), 0, 1\n",
352+
" )\n",
353+
"\n",
339354
" # adding a delay for simulating video processing time\n",
340355
" delay = 0.3 # delay value in seconds\n",
341356
" time.sleep(delay)\n",
342357
" # Capture a frame from the video player - start thread\n",
343358
" frame = cam_stream.read()\n",
344-
" \n",
359+
"\n",
345360
" if acquisition:\n",
346361
" # create filename to next frame\n",
347362
" filename = create_filename(path=(dataset_path / folder))\n",
348363
" cv2.imwrite(filename, frame)\n",
349364
" dType.SetPTPCmdEx(api, 0, Place_X, Place_Y, Place_Z, 0, 1)\n",
350-
" \n",
365+
"\n",
351366
" else:\n",
352367
" # Get the inference results.\n",
353368
" frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)\n",
354369
" # INFERENCE WITH OPENVINO\n",
355370
" predictions = inferencer.predict(image=frame)\n",
356371
" print(predictions.pred_score)\n",
357-
" if predictions.pred_score > 0.48: # modify the threshold depending of your needs\n",
358-
" dType.SetPTPCmdEx(api, 0, Anomaly_X, Anomaly_Y, Anomaly_Z, 0, 1) # define point for abnormalities\n",
372+
" if (\n",
373+
" predictions.pred_score > 0.48\n",
374+
" ): # modify the threshold depending of your needs\n",
375+
" dType.SetPTPCmdEx(\n",
376+
" api, 0, Anomaly_X, Anomaly_Y, Anomaly_Z, 0, 1\n",
377+
" ) # define point for abnormalities\n",
359378
" else:\n",
360379
" dType.SetPTPCmdEx(api, 0, Place_X, Place_Y, Place_Z, 0, 1)\n",
361-
" \n",
380+
"\n",
362381
" dType.SetEndEffectorSuctionCupEx(api, 0, 1)\n",
363382
" j = j + 25\n",
364383
" if j == 75:\n",
@@ -373,7 +392,9 @@
373392
" filename = None\n",
374393
" score = 0\n",
375394
" while True:\n",
376-
" if (dType.gettime()[0]) - time_start >= 0.5: # Time over conveyor belt\n",
395+
" if (\n",
396+
" (dType.gettime()[0]) - time_start >= 0.5\n",
397+
" ): # Time over conveyor belt\n",
377398
" STEP_PER_CRICLE = 360.0 / 1.8 * 10.0 * 16.0\n",
378399
" MM_PER_CRICLE = 3.1415926535898 * 36.0\n",
379400
" vel = float(0) * STEP_PER_CRICLE / MM_PER_CRICLE\n",
@@ -397,7 +418,7 @@
397418
],
398419
"metadata": {
399420
"kernelspec": {
400-
"display_name": "Python 3 (ipykernel)",
421+
"display_name": ".venv",
401422
"language": "python",
402423
"name": "python3"
403424
},
@@ -411,12 +432,7 @@
411432
"name": "python",
412433
"nbconvert_exporter": "python",
413434
"pygments_lexer": "ipython3",
414-
"version": "3.12.7"
415-
},
416-
"vscode": {
417-
"interpreter": {
418-
"hash": "ae223df28f60859a2f400fae8b3a1034248e0a469f5599fd9a89c32908ed7a84"
419-
}
435+
"version": "3.12.8"
420436
}
421437
},
422438
"nbformat": 4,

ai_ref_kits/defect_detection_anomalib/requirements.txt

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,6 @@
33
jupyterlab==4.4.8
44
ipywidgets==8.1.5
55

6-
anomalib[core,openvino]==1.2.0
7-
openvino==2025.3.0
8-
nncf==2.18.0
9-
onnx==1.18.0
10-
scikit-learn==1.6.1
11-
python-dotenv==1.0.1
12-
matplotlib==3.9.4
13-
numpy==1.26.4
6+
anomalib[openvino]==2.2.0
7+
onnxscript==0.5.4
8+
torch==2.8.0

0 commit comments

Comments
 (0)