我想试试ml5.js Pix2Pix example for p5.js.如果我只是复制代码,更新路径,然后try 让它在本地服务器上运行,它就不起作用了.

彼此彼此:

// Copyright (c) 2019 ml5
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT

/* ===
ml5 Example
Pix2pix Edges2Pikachu example with p5.js using callback functions
This uses a pre-trained model on Pikachu images
For more models see: https://github.com/ml5js/ml5-data-and-training/tree/master/models/pix2pix
=== */

// The pre-trained Edges2Pikachu model is trained on 256x256 images
// So the input images can only be 256x256 or 512x512, or multiple of 256
const SIZE = 256;
let inputImg, inputCanvas, outputContainer, statusMsg, pix2pix, clearBtn, transferBtn, modelReady = false,
  isTransfering = false;

function setup() {
  // Create a canvas
  inputCanvas = createCanvas(SIZE, SIZE);
  inputCanvas.class('border-box').parent('canvasContainer');

  // Display initial input image
  inputImg = loadImage('https://ml5js.github.io/ml5-examples/javascript/Pix2Pix/Pix2Pix_promise/images/input.png', drawImage);

  // Selcect output div container
  outputContainer = select('#output');
  statusMsg = select('#status');

  // Select 'transfer' button html element
  transferBtn = select('#transferBtn');

  // Select 'clear' button html element
  clearBtn = select('#clearBtn');
  // Attach a mousePressed event to the 'clear' button
  clearBtn.mousePressed(function() {
    clearCanvas();
  });

  // Set stroke to black
  stroke(0);
  pixelDensity(1);

  // Create a pix2pix method with a pre-trained model
  pix2pix = ml5.pix2pix('https://github.com/ml5js/ml5-library/blob/main/examples/p5js/Pix2Pix/Pix2Pix_callback/models/edges2pikachu.pict', modelLoaded);
}

// Draw on the canvas when mouse is pressed
function draw() {
  if (mouseIsPressed) {
    line(mouseX, mouseY, pmouseX, pmouseY);
  }
}

// Whenever mouse is released, transfer the current image if the model is loaded and it's not in the process of another transformation
function mouseReleased() {
  if (modelReady && !isTransfering) {
    transfer()
  }
}

// A function to be called when the models have loaded
function modelLoaded() {
  // Show 'Model Loaded!' message
  statusMsg.html('Model Loaded!');

  // Set modelReady to true
  modelReady = true;

  // Call transfer function after the model is loaded
  transfer();

  // Attach a mousePressed event to the transfer button
  transferBtn.mousePressed(function() {
    transfer();
  });
}

// Draw the input image to the canvas
function drawImage() {
  image(inputImg, 0, 0);
}

// Clear the canvas
function clearCanvas() {
  background(255);
}

function transfer() {
  // Set isTransfering to true
  isTransfering = true;

  // Update status message
  statusMsg.html('Applying Style Transfer...!');

  // Select canvas DOM element
  const canvasElement = select('canvas').elt;

  // Apply pix2pix transformation
  pix2pix.transfer(canvasElement, function(err, result) {
    if (err) {
      console.log(err);
    }
    if (result && result.src) {
      // Set isTransfering back to false
      isTransfering = false;
      // Clear output container
      outputContainer.html('');
      // Create an image based result
      createImg(result.src).class('border-box').parent('output');
      // Show 'Done!' message
      statusMsg.html('Done!');
    }
  });
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.9.0/p5.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.9.0/addons/p5.dom.min.js"></script>
<script src="https://unpkg.com/ml5@latest/dist/ml5.min.js" type="text/javascript"></script>

<h1>Pix2Pix Edges2Pichaku Example</h1>
<p>1. Wait until the model is loaded</p>
<p>2. Press your mouse to draw a Pikachu on the left side of the canvas.</p>
<p>3. A colored Pikachu image will automatically appear on the right side of the canvas in ~2 seconds. You could also click the "Transfer" button to generate an new image.</p>
<p>4. You could click the "Clear" button to clear the canvas and draw again.</p>
<p id="status">Loading Model... Please wait...</p>
<div class="flex">
  <div>
    <div id="canvasContainer"></div>
    <div id="btnContainer" class="flex flex-space-between">
      <button id="clearBtn">Clear</button><br />
      <button id="transferBtn" class="btn">Transfer</button>
    </div>
  </div>
  <div id="transferContainer">
  </div>
  <div id="output"></div>
</div>

这里还有一个JSFIDLE:

有人知道如何让它运行吗?我会非常感激的.

推荐答案

我想我可以通过一些修补让"回调"示例在本地工作:

  1. 下载示例中的文件:https://github.com/ml5js/ml5-library/tree/main/examples/p5js/Pix2Pix/Pix2Pix_callback
  2. 调整索引.html来加载ml5.来自unpkg的min.js.你的代码中有一个com URL.
  3. 创建一个新函数:
function startTransfer(){
  // Create a pix2pix method with a pre-trained model
  pix2pix = ml5.pix2pix('./models/edges2pikachu.pict', modelLoaded);
}
  1. 将所有呼叫替换为transfer(),但将modelLoaded()中的第一个呼叫替换为startTransfer().
  2. 启动一个简单的本地web服务器;对我来说:python -m http.server个成功了.

这个例子似乎奏效了.我可以在画布上画画,ML模型会根据我添加的新行重新绘制皮卡丘图像.注意,有时初始传输是在加载模板图像(input.png)之前运行的,结果是黄色/红色像素乱码;点击"转移"可以解决这个问题.

基本上,它总是会将模型重新加载到ml5库中;我不知道这对性能的影响,但它在我的浏览器中被相对快速地重新绘制.该文件将被缓存在浏览器中,因此这不是一个问题,但我不确定ml5的内部 struct .js lib和ml5.pix2pix(...)的功能.


我已经把我修改过的代码(包括对JS的一些其他调整)增加到了https://jsfiddle.net/lecrte/jvohcw8r/16/...但它在那里不起作用,因为这些assets资源 相对于HTML文件不可用,而且我们无法加载edges2pikachu.从github直接获取pict.由于CORS问题.

Javascript相关问答推荐

基于变量切换隐藏文本

当试图显示小部件时,使用者会出现JavaScript错误.

无法检测卡片重叠状态的问题

JS,当你点击卡片下方的绿色空间,而它是在它的背后转动时,

Html文件和客户端存储的相关问题,有没有可能?

VUE 3捕获错误并呈现另一个组件

当id匹配时对属性值求和并使用JavaScript返回结果

为什么我的getAsFile()方法返回空?

如何将数组用作复合函数参数?

try 使用PM2在AWS ubuntu服务器上运行 node 进程时出错

如何限制显示在分页中的可见页面的数量

不同表的条件API端点Reaction-redux

使用RxJS from Event和@ViewChild vs KeyUp事件和RxJS主题更改输入字段值

输入的值的类型脚本array.排序()

如果我的列有条件,我如何呈现图标?

如何在移动设备中使用JAVASSCRIPT移除点击时的焦点/悬停状态

表单数据中未定义的数组键

对不同目录中的Angular material 表列进行排序

使用jQuery每隔几秒钟突出显示列表中的不同单词

检测带有委托的元素内部的点击,以及元素何时按其类名被选中