# How to save SVM models trained by python and use it Android for prediction?

**URL:** https://forum.opencv.org/t/how-to-save-svm-models-trained-by-python-and-use-it-android-for-prediction/15862
**Category:** Uncategorized
**Tags:** ml
**Created:** [December 15, 2023, 6:20am UTC](https://forum.opencv.org/t/how-to-save-svm-models-trained-by-python-and-use-it-android-for-prediction/15862 "2023-12-15T06:20:54Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Ranjit\_kw](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/ranjit_kw/32/9291_2.png) [@Ranjit\_kw](https://forum.opencv.org/u/Ranjit_kw)
#### Post date: [December 15, 2023, 6:20am UTC](https://forum.opencv.org/t/how-to-save-svm-models-trained-by-python-and-use-it-android-for-prediction/15862/1 "2023-12-15T06:20:54Z")

</div>

```python
# define svm classifier model
model = SVC(kernel='linear', probability=True)
model.fit(face_embd, labels)

```

how to save it and use it on android kotlin

---

<div class="post-metadata">

### Author: ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)
#### Post date: [December 15, 2023, 6:35am UTC](https://forum.opencv.org/t/how-to-save-svm-models-trained-by-python-and-use-it-android-for-prediction/15862/2 "2023-12-15T06:35:01Z")

</div>

> [@Ranjit\_kw](#):
>
> ```auto
> model=SVC(kernel='linear', probability=True)
> 
> ```

this is not from opencv, we can’t help you

(and besides, what is a `#define’ doing in your python code ?)

however, assuming, you _use_ opencv’s SVM for this,  
you’d save the trained model:

```
svm.save("my.yml")

```

and later, you’d build a new one from the yml:

```
svm = ml.SVM.load("my.yml")

```

main problem on android is, that your saved yml  
will end up somewhere in the assets  
(or elsewhere in the _zipped_ apk,  
where opencv’s C functions cannot read it,  
so 1st thing you need to do is copy it to tmp, sdcard or such)

---

<div class="post-metadata">

### Author: ![Ranjit\_kw](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/ranjit_kw/32/9291_2.png) [@Ranjit\_kw](https://forum.opencv.org/u/Ranjit_kw)
#### Post date: [December 15, 2023, 6:47am UTC](https://forum.opencv.org/t/how-to-save-svm-models-trained-by-python-and-use-it-android-for-prediction/15862/3 "2023-12-15T06:47:03Z")

</div>

#define is a comment line

---

<div class="post-metadata">

### Author: ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)
#### Post date: [December 15, 2023, 8:44am UTC](https://forum.opencv.org/t/how-to-save-svm-models-trained-by-python-and-use-it-android-for-prediction/15862/4 "2023-12-15T08:44:27Z")

</div>

tutorial for OpenCV’s SVM (contains code in three selectable languages):

[https://docs.opencv.org/4.x/d1/d73/tutorial\_introduction\_to\_svm.html](https://docs.opencv.org/4.x/d1/d73/tutorial_introduction_to_svm.html)
