This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import Metal | |
| import MetalKit | |
| class ImageProcessor: NSObject { | |
| //ハードウェアとしてのGPUを抽象化したプロトコル | |
| lazy var device: MTLDevice! = MTLCreateSystemDefaultDevice() | |
| //コマンドバッファの実行順を管理するキュー | |
| var commandQueue: MTLCommandQueue! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func image(from mtlTexture: MTLTexture) -> UIImage { | |
| //画像サイズ | |
| let w = mtlTexture.width | |
| let h = mtlTexture.height | |
| let bytesPerPixel: Int = 4 | |
| let imageByteCount = w * h * bytesPerPixel | |
| let bytesPerRow = w * bytesPerPixel | |
| var src = [UInt8](repeating: 0, count: Int(imageByteCount)) | |
| //CGImageに変換 | |
| let region = MTLRegionMake2D(0, 0, w, h) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public func Run(_ image:UIImage) -> UIImage{ | |
| //GPUで実行されるコマンドを格納するコンテナ | |
| let buffer = commandQueue.makeCommandBuffer() | |
| //コマンドを作成し、コマンドバッファに追加する(エンコード) | |
| let encoder = buffer?.makeComputeCommandEncoder() | |
| encoder?.setComputePipelineState(pipelineState) | |
| //出力用テクスチャ作成 | |
| let textureDescriptor = MTLTextureDescriptor.texture2DDescriptor(pixelFormat: MTLPixelFormat.rgba8Unorm, width:1920, height: 1080, mipmapped: false) | |
| textureDescriptor.usage = [.shaderRead, .shaderWrite] | |
| outTexture = self.device.makeTexture(descriptor: textureDescriptor) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func mtlTexture(from image: UIImage) -> MTLTexture { | |
| //CGImage変換時に向きがおかしくならないように | |
| UIGraphicsBeginImageContext(image.size); | |
| image.draw(in: CGRect(x:0, y:0, width:image.size.width, height:image.size.height)) | |
| let orientationImage = UIGraphicsGetImageFromCurrentImageContext(); | |
| UIGraphicsEndImageContext(); | |
| //CGImageに変換 | |
| guard let cgImage = orientationImage?.cgImage else { | |
| fatalError("Can't open image \(image)") | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //ハードウェアとしてのGPUを抽象化したプロトコル | |
| lazy var device: MTLDevice! = MTLCreateSystemDefaultDevice() | |
| //コマンドバッファの実行順を管理するキュー | |
| var commandQueue: MTLCommandQueue! | |
| public func Setup() { | |
| let defaultLibrary = device.makeDefaultLibrary()! | |
| if let target = defaultLibrary.makeFunction(name: funcName) { | |
| commandQueue = device.makeCommandQueue() | |
| do { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| python retrain.py --image_dir ./images | |
| python label_image.py --image test.jpg --graph output_graph.pb --labels output_labels.txt --input_layer=Placeholder |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| googleimagesdownload -k keywordA -sk keywordB -f "jpg" -s medium |