GIMP-ML/gimp-plugins/monodepth.py

66 lines
2.1 KiB
Python
Raw Normal View History

2020-07-17 04:00:16 +00:00
import os
2020-04-27 04:32:33 +00:00
2020-07-17 04:00:16 +00:00
baseLoc = os.path.dirname(os.path.realpath(__file__)) + '/'
2020-04-27 04:32:33 +00:00
from gimpfu import *
import sys
2020-07-17 04:00:16 +00:00
sys.path.extend([baseLoc + 'gimpenv/lib/python2.7', baseLoc + 'gimpenv/lib/python2.7/site-packages',
baseLoc + 'gimpenv/lib/python2.7/site-packages/setuptools', baseLoc + 'MiDaS'])
2020-04-27 04:32:33 +00:00
2020-07-17 04:00:16 +00:00
from run import run_depth
from monodepth_net import MonoDepthNet
import MiDaS_utils as MiDaS_utils
2020-04-27 04:32:33 +00:00
import numpy as np
import cv2
2020-04-27 04:32:33 +00:00
def getMonoDepth(input_image):
2020-07-17 04:00:16 +00:00
image = input_image / 255.0
2020-08-23 07:44:29 +00:00
out = run_depth(image, baseLoc+'weights/MiDaS/model.pt', MonoDepthNet, MiDaS_utils, target_w=640)
2020-07-17 04:00:16 +00:00
out = np.repeat(out[:, :, np.newaxis], 3, axis=2)
d1,d2 = input_image.shape[:2]
out = cv2.resize(out,(d2,d1))
# cv2.imwrite("/Users/kritiksoman/PycharmProjects/new/out.png", out)
return out
def channelData(layer): # convert gimp image to numpy
region = layer.get_pixel_rgn(0, 0, layer.width, layer.height)
pixChars = region[:, :] # Take whole layer
bpp = region.bpp
2020-04-27 04:32:33 +00:00
# return np.frombuffer(pixChars,dtype=np.uint8).reshape(len(pixChars)/bpp,bpp)
2020-07-17 04:00:16 +00:00
return np.frombuffer(pixChars, dtype=np.uint8).reshape(layer.height, layer.width, bpp)
2020-04-27 04:32:33 +00:00
2020-07-17 04:00:16 +00:00
def createResultLayer(image, name, result):
rlBytes = np.uint8(result).tobytes();
rl = gimp.Layer(image, name, image.width, image.height, image.active_layer.type, 100, NORMAL_MODE)
region = rl.get_pixel_rgn(0, 0, rl.width, rl.height, True)
region[:, :] = rlBytes
image.add_layer(rl, 0)
2020-04-27 04:32:33 +00:00
gimp.displays_flush()
2020-07-17 04:00:16 +00:00
def MonoDepth(img, layer):
gimp.progress_init("Generating disparity map for " + layer.name + "...")
2020-04-27 04:32:33 +00:00
imgmat = channelData(layer)
2020-07-17 04:00:16 +00:00
cpy = getMonoDepth(imgmat)
createResultLayer(img, 'new_output', cpy)
2020-04-27 04:32:33 +00:00
register(
"MonoDepth",
"MonoDepth",
"Generate monocular disparity map based on deep learning.",
"Kritik Soman",
"Your",
"2020",
"MonoDepth...",
2020-07-17 04:00:16 +00:00
"*", # Alternately use RGB, RGB*, GRAY*, INDEXED etc.
[(PF_IMAGE, "image", "Input image", None),
(PF_DRAWABLE, "drawable", "Input drawable", None),
],
2020-04-27 04:32:33 +00:00
[],
MonoDepth, menu="<Image>/Layer/GIML-ML")
2020-07-17 04:00:16 +00:00
main()