File size: 3,106 Bytes
34097e9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# controlnet + txt2img\n",
    "# enable `Allow other script to control this extension` in settings\n",
    "\n",
    "import requests\n",
    "import cv2\n",
    "from base64 import b64encode\n",
    "\n",
    "def readImage(path):\n",
    "    img = cv2.imread(path)\n",
    "    retval, buffer = cv2.imencode('.jpg', img)\n",
    "    b64img = b64encode(buffer).decode(\"utf-8\")\n",
    "    return b64img\n",
    "\n",
    "b64img = readImage(\"/root/workspace/nahida/0e17302b9bfa15402f783c29c0d1d34f.jpg\")\n",
    "\n",
    "class controlnetRequest():\n",
    "    def __init__(self, prompt):\n",
    "        self.url = \"http://localhost:7860/controlnet/txt2img\"\n",
    "        self.body = {\n",
    "            \"prompt\": prompt,\n",
    "            \"negative_prompt\": \"\",\n",
    "            \"seed\": -1,\n",
    "            \"subseed\": -1,\n",
    "            \"subseed_strength\": 0,\n",
    "            \"batch_size\": 1,\n",
    "            \"n_iter\": 1,\n",
    "            \"steps\": 15,\n",
    "            \"cfg_scale\": 7,\n",
    "            \"width\": 512,\n",
    "            \"height\": 768,\n",
    "            \"restore_faces\": True,\n",
    "            \"eta\": 0,\n",
    "            \"sampler_index\": \"Euler a\",\n",
    "            \"controlnet_input_image\": [b64img],\n",
    "            \"controlnet_module\": 'canny',\n",
    "            \"controlnet_model\": 'control_canny-fp16 [e3fe7712]',\n",
    "            \"controlnet_guidance\": 1.0,\n",
    "        }\n",
    "\n",
    "    def sendRequest(self):\n",
    "        r = requests.post(self.url, json=self.body)\n",
    "        return r.json()\n",
    "\n",
    "js = controlnetRequest(\"walter white\").sendRequest()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import io, base64\n",
    "import matplotlib.pyplot as plt\n",
    "from PIL import Image\n",
    "\n",
    "pil_img = Image.open('/root/workspace/nahida/0e17302b9bfa15402f783c29c0d1d34f.jpg')\n",
    "image = Image.open(io.BytesIO(base64.b64decode(js[\"images\"][0])))\n",
    "mask_image = Image.open(io.BytesIO(base64.b64decode(js[\"images\"][1])))\n",
    "\n",
    "plt.figure()\n",
    "f, axarr = plt.subplots(1,3) \n",
    "axarr[0].imshow(pil_img)   \n",
    "axarr[1].imshow(image)  \n",
    "axarr[2].imshow(mask_image)  "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "pynb",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.9"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "d73345514d8c18d9a1da7351d222dbd2834c7f4a09e728a0d1f4c4580fbec206"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}