controlnetOverMask / image_process.py
hahahafofo's picture
Duplicate from hirol/controlnetOverMask
63da23a
# This is a sample Python script.
# Press ⌃R to execute it or replace it with your code.
# Press Double ⇧ to search everywhere for classes, files, tool windows, actions, and settings.
import numpy as np
import cv2
import base64
def cv2_base64(image):
base64_str = cv2.imencode('.png',image)[1].tobytes()
base64_str = base64.b64encode(base64_str)
return base64_str.decode('utf-8')
def base64_cv2(base64_str):
imgString = base64.b64decode(base64_str)
nparr = np.frombuffer(imgString,np.uint8)
image = cv2.imdecode(nparr,cv2.IMREAD_COLOR)
return image
def image_pose_mask(imagepath : str):
img = base64_cv2(imagepath)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(gray, 10, 255, cv2.THRESH_BINARY)
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnt = contours[-1]
hull = cv2.convexHull(cnt)
length = len(hull)
if length > 4:
for i in range(length):
cv2.line(img, tuple(hull[i][0]), tuple(hull[(i + 1) % length][0]), (255, 255, 255), 2)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
_, threshold = cv2.threshold(gray, 10, 255, cv2.THRESH_BINARY)
mask = img.copy()
kernel = np.ones((30, 30), dtype=np.uint8)
dilated = cv2.dilate(threshold, kernel, 20)
dilated = cv2.dilate(dilated, kernel, 20)
contours, hierarchy = cv2.findContours(dilated, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
valid = len(contours) > 0
area = []
for k in range(len(contours)):
area.append(cv2.contourArea(contours[k]))
max_idx = np.argmax(np.array(area))
mask2 = cv2.drawContours(mask, contours, max_idx, (255, 255, 255), thickness=-1)
img_base64 = cv2_base64(mask2)
return img_base64
def image_pose_mask_numpy(image):
img = np.uint8(image)
print("shape:" + str(img.shape))
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(gray, 10, 255, cv2.THRESH_BINARY)
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnt = contours[-1]
hull = cv2.convexHull(cnt)
length = len(hull)
if length > 4:
for i in range(length):
cv2.line(img, tuple(hull[i][0]), tuple(hull[(i + 1) % length][0]), (255, 255, 255), 2)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
_, threshold = cv2.threshold(gray, 10, 255, cv2.THRESH_BINARY)
mask = img.copy()
kernel = np.ones((30, 30), dtype=np.uint8)
dilated = cv2.dilate(threshold, kernel, 20)
dilated = cv2.dilate(dilated, kernel, 20)
contours, hierarchy = cv2.findContours(dilated, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
valid = len(contours) > 0
area = []
for k in range(len(contours)):
area.append(cv2.contourArea(contours[k]))
max_idx = np.argmax(np.array(area))
mask2 = cv2.drawContours(mask, contours, max_idx, (255, 255, 255), thickness=-1)
img_base64 = cv2_base64(mask2)
return img_base64
def image_canny(imagepath:str):
img = base64_cv2(imagepath)
image_map = cv2.Canny(img, 100, 200)
contours, hierarchy = cv2.findContours(image_map, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnt = contours[-1]
hull = cv2.convexHull(cnt)
length = len(hull)
if length > 4:
for i in range(length):
cv2.line(img, tuple(hull[i][0]), tuple(hull[(i + 1) % length][0]), (0, 0, 255), 2)
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
contours, hierarchy = cv2.findContours(img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
valid = len(contours) > 0
area = []
for k in range(len(contours)):
area.append(cv2.contourArea(contours[k]))
max_idx = np.argmax(np.array(area))
mask2 = cv2.drawContours(image_map, contours, max_idx, (0, 0, 255), thickness=3)
img_base64 = cv2_base64(mask2)
return img_base64
# Press the green button in the gutter to run the script.
if __name__ == '__main__':
image_canny(imagepath='download (13).png')